Casting of Primitives in Java

Casting is the conversion of data of one type to another type either implicitly or explicitly.  

Casting happens for both primitive types and reference types. 

If the casting operation is safe, java will do automatic type casting. This is called implicit type casting. 

If java can't be sure whether the casting will be safe, java will not do automatic casting, but programmer can do the casting if he is sure about the outcome. This is called explicit type casting.

Both types should be compatible in both implicit and explicit casting. We cannot cast two unrelated classes  implicitly or explicitly. 

 

Automatic type casting for primitives

In case of primitives, a smaller data type can be assigned to a bigger data type. 

There are eight primitive data types defined in Java - byte, short, int, long, float, double, char and boolean. 

The types byte, short, int, long, float, double and char are compatible with each other and size increases in the order: byte, short, int/char, long, float, double. A lower size type will be implicitely converted to a higer size, but not the opposite.

The type boolean is not compatible with any other type. 

These implicit conversions are referred to as a widening as the data is being assigned from a less precise data type to a more precise data type. With a widening operation, the use of cast operator is optional. 

 

Implicit conversion summary for primitives

  • byte can be converted implicitely to short, int, long,float and double; not to boolean and char.

  • short can be converted implicitely to int, long, float and double; not to byte,boolean and char.

  • int can be converted implicitely to long, float and double; not to byte, short, boolean and char.

  • long can be converted implicitely to float and double; not to byte, short, int, boolean and char.

  • float can be converted implicitely to double; not to byte, short, int, long, boolean and char.

  • double can not be converted implicitely to any primitive types.

  • char can be converted implicitely to int, long, float and double; not to byte, short and boolean.

  • boolean can not be converted implicitely to any primitive types.

 

In case of reference types, a child object or child reference variable can be assigned to a parent reference. Parent can be a class, abstract class or interface. 

 

Explicit type casting

The types should be compatible for both implicit and explicit casting.

A lower size type will be implicitely converted to a higer size, but not the opposite. A higher size compatible type can be explicitely cast to a lower size type if we are sure that the value of the higher size type is within the range of lower size type. 

If the data is being assigned from a more precise data type to a less precise data type, as in this case, the casting is referred to as narrowing

long lv = 10;

int iv = (int) lv;

System.out.println(iv);

This will print 10.

If the  value within the higher size type is not within the range of lower size type, data loss might happen. Range of int is - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) inclusive and range of long is -9,223,372,036,854,775,808 (-2^63) to 9,223,372,036,854,775,807 (2^63 -1) inclusive. 

long longVar = 9223372036854775807L;

System.out.println("longVar="+longVar);

int intVar = (int) longVar;

System.out.println("intVar="+intVar);

This will print:

longVar=9223372036854775807

intVar=-1

Type boolean is not compatible with any other type and hence cannot be casted even explicitely to other primitive types.  

Another example is, if a floating point number is assigned to an integer, the fractional part is lost. 

int intVar;

float floatVar = 1.5F;

intVar = (int) floatVar; // Cast a float to an int

System.out.println(intVar);

This will print: 1

Comments

There is a typo error in example of Explicit type casting.

A aa = new B();

B bb = (B)aa; // it is not giving ClassCastException at runtime. So according to me

it should be like this

A aa = new A();

B bb = (B)a;// then this line is giving ClassCastException.

Was it useful?

Thank you Praveen. I have corrected the note above and also added 25 points to your account as a thank you gift.

Was it useful?

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!