Short Variable in Java Programming
Java Basic Concepts
In this lesson, we will learn, what is Short Variable and how it works in Java programming along with some examples.
What is Short Variable
The another integer data type which is available in Java is short. It can store values within the range -32768 to 32767. The short data type can be useful in storing numbers in large array to save memory in computer.
Syntax of Declaring Short Variable in Java
Here short is used for Short data type and variable_name is the name of variable (you can use any name of your choice for example: a, b, c, alpha, etc.) and ; is used for line terminator (end of line).
Now let's see some examples for more understanding.
Declare a short variable x.
Declare a short variable x to assign a short value 1024.
short x = 1024;
Declare 3 short variables x, y and z in a single line.
short x, y, z;
Declare 3 short variables x, y and z to assign the short value 4297, 670 and 8531 respectively in a single line.
short x=4297, y=670, z=8531;
Declare a short variable x and assign the short value 180 in the second line.
short x; x = 180;
Declare a short variable x and assign the short value 7654 and change its value to 548 in the next line.
short x = 7654; x = 548; // now the new value of x is 548
Test Your Knowledge
Attempt the multiple choice quiz to check if the lesson is adequately clear to you.