short, int and long primitive data type

short, int and long are similar data types with different range.

Example Program: When we override a method which takes arguments short, int and long like below. Always by default int argument method will be called. if we want to call a method from other argument type like long or short we need to do type casting.

public class IntegerDataTypes {
    public void testInt(short s){
        System.out.println("short method is called "+s);
    }
    
    public void testInt(int i){
        System.out.println("int method is called "+i);
    }
    
    public void testInt(long l){
        System.out.println("long method is called "+l);
    }
}

 

public class TestIntegerDataType {
    public static void main(String args[]){
        IntegerDataTypes in = new IntegerDataTypes();
        int s = 25;
        in.testInt((short)s);
        in.testInt(s);
        in.testInt((long)s);
    }
}

Output:

short method is called 25
int method is called 25
long method is called 25

Leave a comment