Monday, 30 September 2013

Type Conversions and Casting in Java

          The type conversion is the process by which one data typed variable value is converted into other data typed variable value. For example an int data type is converted into a float data type, etc.

          There can be two kinds of type conversion.

1.     Implicit Conversion
2.     Explicit Conversion

  •         Implicit Conversion:

                         This type of conversion takes place automatically when the following conditions are satisfied.

1.     Destination variable type and source variable type are compatible.
2.     Size of destination type of variable is large enough to hold the values of source type variable.  
            Consider the below figure, u can see float labeled box is larger than the int box and int box is larger than byte box. The smaller box can occupy in bigger box but other way, that is larger into smaller is not possible.

Code fragment showing implicit conversion

   byte b=20; // declare a byte variable
   int a=b; // byte is converted and stored in int.
int a=68; // declare int
long lng=a; //int is converted and stored in long. 

Example Program for Implicit Type Conversion:

/** Program for Implicit type conversion
 * @author Jaivanth
public class SampleImplicitTypeConversion
    public static void main(String[] args)
               byte b=20;// declare byte.
               char ch='A';// character declared

               int a=b;// implicit type conversion.
               int z=ch; // char assigned to z
               System.out.println("Value of z is "+z);
               float f=a;//implicit type conversion.
               double db=b+a+f+ch; // implicit type conversion in expression.
               System.out.println("value of double is "+db);

Value of z is65
value of double is125.0

  •         Explicit Conversion:

                      This type of conversion takes place between the incompatible types. For example, conversion from int to byte or float to int. This conversion is not possible automatically. In order achieve explicit conversion you should use ‘cast’.

Code fragments showing type casting:

int a=10; //declared int variable.

byte b=(byte)a; //int value converted to byte value using cast and stored in byte.

Float f=20.58; //declared float.

int a=(int)f; // float value type casted and stored in int.

Example Program for Explicit Type Conversion: 

/** program for explicit type conversion
 * @author Jaivanth
public class SampleExplicitTypeConversion
   public static void main(String[] args)
             double db=65.69;// double declared
             int a=(int)db;// type casting from double to int
             char ch=(char)a;// type casting from int to char
             byte b=(byte)a; // type casting from int to byte
             int z=a+ch+b;  // dynamic initialization
             System.out.println("Value of db is "+db);
             System.out.println("Value of ch is "+ch);
             System.out.println("Value of a is "+a);
             System.out.println("Value of b is "+b);
             System.out.println("Value of z is "+z);

Value of db is 65.69
Value of ch is A
Value of a is 65
Value of b is 65
Value of z is 195