The integer data type in Java is a whole-number data type that consumes 32 bits of memory and can hold values between approximately negative and positive two billion. The short data type consumes half the space and can hold values between approximately negative and positive 32,000. Most of the time, the extra numerical accuracy is worth the space cost, but in large arrays of data, short can save a lot of space. Like other primitive data types, short also has a helper class called "Short" that contains useful methods for working with shorts and converting them to other data types.
Open Netbeans or any other Java Integrated Development Environment.
Click "File" and "New class."
Type "psvm." The Java IDE will expand this into a valid main method.
Add the following code to the main method to define a short and cast it to integer:
short s = 12;
int i = Short.intValue(s);
Tips & Warnings
- You can also cast from short to int using the more traditional C-style syntax: "int i = (int) s;".