The short keyword in Java programming language
In this tutorial, we learn about the short keyword in java programming language.
The short keyword is a Java keyword used to declare a type of numeric variable. A short variable can hold a 16-bit integer number which ranges from -32,768 to 32767
Declaration
short – in variable
the syntax of short keyword
short<variable_name>=<short_value>;
Example
Example of short keyword
short length = 2345;
Program 1
//Example for short variable public class shortEx1{ public static void main (String args[]){ short hight=20456; System.out.println("mountain ight is :"+hight); } }
When we execute the above program, it produces the following result
My Total_marks is: 789
short – method
The short keyword can be used to declare the return type of a method in Java
Example
Example of short keyword
public short getHeight(){ return 2345; }
Above method is a short method which returns any value.
Example short method within a class
public class Example _short{ // this is a class in java public static void main(String args[]){ // this is a main method in java } public short add(){ // this is a short method which returns value; } }
program 2
public class shortEx2{ public static void main (String args[]){ dis_town(); } static short dis_town(){ //short keyword is used to declare method short distance=20000; System.out.println("town distance is :"+distance); return distance;//return statements } }
When we execute the above program, it produces the following result
town distance is: 20000
There are other Java language keywords that are similar to this keyword
Suggested for you