Ticker

6/recent/ticker-posts

Type Casting in JAVA | Implicit and Explicit Type Casting

Java supports two types of castings – primitive data type casting and reference type casting. Reference type casting is nothing but assigning one Java object to another object. It comes with very strict rules and is explained clearly in Object Casting(next topic). Now let us go for data type casting.

a) data type casting 

Java data type casting comes with 3 flavors. 

1. Implicit casting 

2. Explicit casting 

3. Boolean casting. 

1. Implicit casting (widening conversion) 

A data type of lower size (occupying less memory) is assigned to a data type of higher size. This is done implicitly by the JVM. The lower size is widened to higher size. This is also named as automatic type conversion. 

class Vikas 

        public static void main(String[] args) 
        
                int a = 10; 
                double d = a; 
                System.out.println(d); //prints 10.0 
        

2. Explicit casting (narrowing conversion) 

A data type of higher size (occupying more memory) cannot be assigned to a data type of lower size. This is not done implicitly by the JVM and requires explicit casting; a casting operation to be performed by the programmer. The higher size is narrowed to lower size. 

class Vikas 

        public static void main(String[] args) 
        
                double d = 10.89; 
                int c = d ; //Error 
                double d = 10.89; 
                int c = d ; //Error 
                int a = (int)d; //explicit casting 
         } 

3. Boolean casting 

A boolean value cannot be assigned to any other data type. Except boolean, all the remaining 7 data types can be assigned to one another either implicitly or explicitly; but boolean cannot. We say, boolean is incompatible for conversion. 

byte –> short –> int –> long –> float –> double 

In the above statement, left to right can be assigned implicitly and right to left requires explicit casting. That is, byte can be assigned to short implicitly but short to byte requires explicit casting. 

b) Reference type casting 

Subclass object can be assigned to a super class object and this casting is done implicitly. This is known as upcasting (upwards in the hierarchy from subclass to super class) 

Eg : Father f = new Son();//implicit casting 

Java does not permit to assign a super class object to a subclass object (implicitly) and still to do so, we need explicit casting. This is known as down casting (super class to subclass). Down casting requires explicit conversion. In down casting , first you need to do the upcasting after upcasting than only u can perform the down casting. Or else its a classCastException 

Eg : Father f = new Son(); //upcasting 

Son s = (Son) f; //down casting 

Son s = (Son) new Father(); //compiles but at runtime give exception

class Father 

        void factory() 
        
                System.out.println("Parent factory()"); 
        
        void home() 
        
                System.out.println("some home()"); 
        

class Son extends Father 

        void factory() 
        
                System.out.println("modified Parent factory() by Son"); 
        
        void home() 
        
                System.out.println("modified Parent home() by Son"); 
        
        void car() 
        
                System.out.println("Son alone has car()"); 
        

ALSO SEARCH:

"implicit and explicit type casting in java"

"develop a program to show the use of explicit type casting"

"type conversion and casting in java"

"implicit type casting"

"object type casting in java"

"non primitive type casting in java"

"why type casting is required in java"

"write two examples of implicit type casting in java"