Short Data Type in Java

Short Data Type in Java | As we all know there are two types of data types in java, primitive and non-primitive data types. Primitive data types are fundamental data types whereas the non – primitive data types are derived data types. Among them short is one of the primitive data types in Java which basically belongs to the integer type.

Integer has been divided into four data types again based on the sizes they can store:- byte, short, int, and long. Short stores numeric values, there might be a requirement to store lesser values than the int data type or greater values than the byte data type. Now coming to short data type, it can store values up to 16 bits (2 Bytes).

What is Short Data Type in Java?

The short data type in Java is used to store the values from -32768 to 32767. The range of short data type is -215 to 215 – 1.  Using the primitive data type Integer the short, byte, and long has been divided based on their sizes and range. The short data type is used to store the lesser value or the values which consume less memory space. The size of the short data type is 2 bytes only and the default value is 0.

Size :: 2 bytes
Range ::  -215 to 215-1 
          Or,
          -32,768 to 32,767
Default value :: 0
Corresponding Wrapper class :: Short

It is the most rarely used data type in the Java language. The short data type is best suitable for 16-bit microprocessors like 8085 but currently, they are outdated and we have better alternatives. Since 16-bit microprocessors are not used therefore short data type is also outdated.

How To Declare Short In Java

The syntax for declaring a short variable is as follows:-
Short variable = value;

Example:
short number = 122;

To assign a short data type value to some variable, we don’t need to use any suffix like we use ‘L’ for the long data type. See more:- Long Data Type In Java

Short Data Type in Java Example

short s1 = 32767; // valid
short s2 = 32768; // error
short s3 = 'a'; // valid
short s4 = 900.5; // error
short s4 = false; // error
// boolean cannot be converted to short

As we all know we cannot store the numeric values in the quotes, that is, storing the value as shown below would be invalid.

short num1 = 10; // valid
short num2 = '10'; // invalid

To represent an integer number (example 9) as short we must explicitly write the below code.

short n2 = 9; // variable assignment  
Or, 
(short)9; // casting 
// it will convert 9 as int to short

Java Program Example for Short Data Type

public class Main {
    public static void main(String[] arg) {
        Short number = 789;
        Short number1 = -2356;
        System.out.println("Positive short number: " + number);
        System.out.println("Negative short number: " + number1);
    }
}

Output:-

Positive short number: 789
Negative short number: -2356

public class Main {
    public static void main(String[] arg) {
        Short num1 = 152;
        Short num2 = 23;
        System.out.println("Sum: " + (num1 + num2));
        System.out.println("Minus: " + (num1 - num2));
        System.out.println("Divide: " + (num1 / num2));
        System.out.println("Multiply: " + (num1 * num2));
        System.out.println("Remainder: " + (num1 % num2));
    }
}

Output:-

Sum: 175
Minus: 129
Divide: 6
Multiply: 3496
Remainder: 14

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *