Type Casting In Java

Type casting in java is used to convert data from one data type to another data type.
When you assign value of one data type to another data type, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion and if not then they need to be casted or converted explicitly. For example, assigning an int value to a long variable.

There are two types of casting,

1) Primitive Casting.

2) Derived Casting


Primitive Casting

Primirive Casting is used to convert data from one primitive data type to another primitive data type.

These are data types with no decimal places and with decimal places..

1) byte     2) short     3) int      4) long       5) float     6) double

When you put them in the increasing order of their memory size, you get

byte < short < int < long < float < double.
byte < short < int < long < float < double.

Please remember this order we will be using this order in below examples. byte is the smallest data type and double is the biggest data type in terms of memory size.

There are two types in primitive casting. 

  1. Auto Widening  
  2.  Explicit Narrowing

1) Auto Widening

When you are converting data from small sized data type to big sized data type, i.e when you are converting data from left-placed data type to right-placed data type in the above order, auto widening will be used. For example, when you are converting byte to short or short to int, auto widening will be used.

Go through this example.

class AutoWidening{
    static float methodOne(int i){
        long j = i;     //int is auto widened to long
        return j;       //long is auto widened to float
    }

    public static void main(String[] args){

        byte b = 10;

        short s = b;      //byte is auto widened to short

        double d = methodOne(s);    //short is auto widened to int and float to double

        System.out.println(d);

    }

}


When you are converting data from big sized data type to small sized data type, i.e when you are converting data from right-placed data type to left-placed data type in the above order, explicit narrowing will be used. For example, when you are converting double to float or float to int, explicit narrowing will be used

2) Narrowing or Explicit Conversion

When you are converting data from big sized data type to small sized data type, i.e when you are converting data from right-placed data type to left-placed data type in the above order, explicit narrowing will be used. For example, when you are converting double to float or float to int, explicit narrowing will be used.

When you put them in the increasing order of their memory size, you get

byte < short < int < long < float < double.
 double  >  float  >  long >  int  >  short   >  byte

 

class ExplicitlyNarrowing{

    static short methodOne(long l){

        int i = (int) l;     //long is explicitly narrowed to int
        return (short)i;       //int is explicitly narrowed to short

    }

    public static void main(String[] args){

        double d = 10.25;
        float f = (float) d;      //double is explicitly narrowed to float
        byte b = (byte) methodOne((long) f);    //float is explicitly narrowed to long and short to byte
Sytem.o
        System.out.println(b);

    }

}

 

Related Articles

post a comment