short data type in Java
The short data type is a type of integer data type just like int and byte. However, short has 16 bits of memory. That’s , short is twice bigger than byte (8 bits) but half of int (32 bits).
nanadwumor
![Primitive Data types.-short Primitive Data types.-short](https://villagecoder.com/wp-content/uploads/2023/01/Primitive-Data-types.-short.jpg)
- The short data type is an integer.
- short comprises 16 bits of memory.
-
The range of possible values of short is -32,768 to 32,767.
RECOMMENDED ARTICLES
Variables and Literals in Java
Variables are used in many situations. For Instance, game applications use variables to collect your name when you sign in; to store and keep track of your number of trials, successes and failures. In Java, a variable is a container used to store...
double data type in Java
The double data type is also a floating point number. It is called double because it has a double-precision decimal number. The double data type is a floating point number. It is double precision data type The range is 4.9406564584124654 x...
The print and println methods in Java
The print() and println() methods are Java's predefined methods used to display text output on the console. These methods are part of the Java API. The print and println methods are used to print string of characters onto the console The print...
If we want to save memory and yet byte is too small for our value, we can use short data type which is halfway between byte and int.
The range of possible values of short is -32,768 to 32,767.
Join Other Subscribers on Our YouTube Channel and Don’t Miss a thing!
Declaring a short variable
A short variable is declared just as an int or byte variable. Examples include:
short age;
short height;
short weight;
Alternatively, you can also declare all short variables in a row. That’s,
short age, height, weight;
Like an int, the default variable of a short is 0.
Program demonstrates short data type
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Program demonstrates the short data type public class ShortVariable { public static void main(String[] args) { short amount=400; //'amount' is declared as a short data type. It is in short range (32,768 to 32,767.) short remaining = -3000; //'remaining' is declared as a short data type. It is in short range (32,768 to 32,767.) System.out.println("My account balance used to be $" + amount + " but it's now " + remaining); } } |
Output
![program demonstraates the short data type in java program demonstraates the short data type in java](https://villagecoder.com/wp-content/uploads/2023/01/program-demonstrates-the-short-data-type-in-java.jpg)
![program demonstraates the short data type in java program demonstraates the short data type in java](https://villagecoder.com/wp-content/uploads/2023/01/program-demonstrates-the-short-data-type-in-java.jpg)
NOTE: If the value you assign to a short variable is out of the range of the short data, that’s -32,768 to 32,767, Java treats the value as an int. Example,
short amount=33000; //Error! mismatch here
amount is a short but 33000 is beyond the range of a short. The Java compiler thus treats 33000 as an int data type as a result. And an int value (with a bigger range of memory) cannot be assigned to or stored in a short data type (of small memory).
Besides, Java is a strongly typed Programming language. That’s, everything on the left and right of the assignment operator (equal sign) must match.
When the value is out of int range
We talked about the int data type. By default, all integer values out of range of its intended data type are treated as int by the Java compiler. Let’s look at examples;
byte price=130; //Error! mismatch here
130 is out of range (-128 to 127) of byte. Java therefore treats the 130 as an int. This is because int is the default integer data type in Java
On the left, because price is a byte, while the right, 130 is considered an int. An int value is too large to be stored in a byte.
Thus, there will be type mismatch and an error will ensue.
short distance=33000; //Error! Type mismatch here
33000 is out of range(-32,768 to 32,767) of short. Thus, Java treats 33000 as an int. On left we have a short variable and on right we have int. There’s type mismatch. We cannot store int of bigger memory range into a smaller memory range like a short.
But what if the int data itself Java uses as default integer type goes out of its range? When it happens, the next bigger integer type is called long but Java does not convert automatically. You have to explicitly tell the compiler. That’s,
int distance=3147483647; //Error! Type mismatch here
The compiler will complain that the number 3147483647 is out of range. It is bigger than what the int variable can store in memory (−2,147,483,648 to +2,147,483,647)
To resolve this, we either reduce the number’s value or use the long data type. That’s,
long distance=3147483647; //Error! Type mismatch here
The distance variable is now declared as long. It can now hold the number 3147483647. Unfortunately, the compiler will still complain and it is right so because 3147483647 is by default treated as an int but it’s out of int range.
To force the compiler to recognize 3147483647 as a long data type, we need to append l in Lowercase or uppercase (L). That’s, either of the two statements below is correct.
long distance=3147483647L; //Correct
long distance=3147483647l; //Correct
Like all other primitive data types, the print and println methods can print the short data type.
You May Also Like…
Variables and Literals in Java
Variables are used in many situations. For Instance, game applications use variables to collect...
double data type in Java
The double data type is also a floating point number. It is called double because it has a...
The print and println methods in Java
The print() and println() methods are Java's predefined methods used to display text output on the...
0 Comments