Type Casting or Conversion in Java

In Java language type casting or conversion means convert the value of  one data type into another compatible data type variable. In simple word type casting or type conversion means convert int data type value  into long data type.

Two conditions for type casting and conversion:

  1. Both types are compatible e.g int into long.
  2. Destination type is larger then source type.
All data types are not compatible and all types of conversions all not allowed automatic or implicitly.
For example you can not convert double into byte data type.
                             But......
It is still possible in java to convert incompatible data types by using cast., cast perform explicit conversion between two incompatible data types.  

Automatic type conversion:

Convert automatically two types like this:

byte a=1;
byte b=12;
int sum=a+b;

 Incompatible Types conversion by Casting:

int a;
byte b;
// code e.g example sum
b= (byte ) a;



Comments

Popular posts from this blog

Inheritance in Java

Operators in Java Language?