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.


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;


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;


int intVar = (int) longVar;


This will print:



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


This will print: 1


Important Note


Both types should be compatible in both implicit and explicit casting. We cannot cast two unrelated classes  implicitly or explicitly. Except boolean, all other types are compatible with each other. A boolean can be assigned only a true or false or an expression that evaluate to true or false.

Example: Valid boolean assignments

boolean b1 = true;

boolean b2 = false;

boolean b3 = (3 < 5) ;

Example: Invalid boolean assignments

boolean b4 = 0;

boolean b2 = 1;




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?

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)