Site icon Codeforcoding

The short keyword in Java programming language

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

 byte  in Java Language

 int  in Java Language

long  in Java Language

 float  in Java Language

char  in Java Language

 void  in Java Language

try  in Java Language

Catch in Java Language

implements  in java Language

public  in Java Language

static  in Java language

Volatile  in Java Language

Package  in Java Language

class  in Java Language

boolean  in Java Language

break in Java Language

do  in Java Language

double  in Java Language

This  in Java Language

final  in Java Language

switch  in Java Language

interface  in Java Language

extends  in Java language

 

Suggested for you

 Java language Keywords

C language Keywords

 Python language Keywords

keywords in C language

Finally  in Java language

The int keyword in Java programming language
The long keyword in Java programming language
Exit mobile version