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
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s