Java short keyword
The Java short keyword is a primitive data type. It is used to declare variables. It can also be used with methods. It can hold a 16-bit signed two’s complement integer.
Points to remember
- The short contains minimum value of -32,768 and a maximum value of 32,767 (inclusive).
- Its default value is 0.
- Its default size is 2 byte.
- It is used to save memory in large arrays.
Examples of Java byte keyword
Example 1
Let’s see an example to use short data type with positive and negative value.
Output:
num1: 10 num2: -10
Example 2
Let’s see an example to check whether the short data type holds decimal value.
Output:
error: incompatible types: possible lossy conversion from double to short
Example 3
Let’s see an example to check whether the short data type holds float value.
Output:
error: incompatible types: possible lossy conversion from float to short
Example 4
Let’s see an example to check whether the short data type holds char value. In such case, the compiler typecast the character implicitly to short type and returns the corresponding ASCII value.
Output:
num: 97
Example 5
In this example, short data type holds the minimum and maximum value.
Output:
min: -32768 max: 32767
Example 6
Let’s create a method that returns a short type value.
Output:
10