Ads Top

Type Casting in Java

Overview:

Type Casting in Java occurs when the type of a value is cast to another type. For example, a primitive value can be cast to an equivalent value of double type. In this blog post, we will deep dive into Type Casting in Java.

Table of Contents:

  1. Types of Casting in Java
  2. Explicit/Narrowing Casting
  3. Implicit/Widening Casting
  4. Type Casting between Object References 

Types of Casting in Java

Type Casting can be performed primitive to primitive or between object reference. There are two types of casting in java, explicit casting, and implicit casting.

Explicit Casting(Narrowing Casting)

Explicit casting also know Narrowing casting can be performed with the below syntax:

(type) exp

where type is what we would like to have in the resulting expression (exp).

Narrowing Casting means, when a larger type is converted to the smaller type. For example,
when double is converted to an integer.

Consider the following code:

 public class TypeCasting {  
   public static void main(String[] args) {  
     System.out.println("Explicit/Narrowing Casting:");  
     double d = 10;  
     int i = (int) d;  
     System.out.println(i);  
   }  
 }  

Output:
 Explicit/Narrowing Casting  
 10  

Implicit/Widening Casting

Widening cast means when a smaller type is converted to a larger type. This is done automatically hence we do not need parenthesis in front of the value.

For example, consider the following code:

 public class TypeCasting {  
   public static void main(String[] args) {  
     System.out.println("Implicit/Widening Casting:");  
     int i = 10;  
     double d = i;  
     System.out.println(d);  
   }  
 }  

Output:
 Implicit/Widening Casting:  
 10.0  

However, during narrowing, implicit cast is not allowed in java. For example, the below code will throw a compilation error: possible loss of precision

double d = 34.5;
int i = d;

Type Casting between Object References

Object references can be cast between each other if they share the same inheritance or interface. This means an object reference can be cast to its own type or subclass or superclass. Type Casting between object references is categorized into the following:
  • Upcasting
  • Downcasting 

Upcasting
When an object type is cast to its superclass type, it is called upcasting. Upcasting does not require the use of the cast operators.

For example, Let's say we have an Animal as a parent class and Dog as a child class of Animal class.

Animal.class  
 package typecasting;  
 public class Animal {  
   public String eat(){  
     return "An animal is eating";  
   }  
 }  

Dog.class
 package typecasting;  
 public class Dog extends Animal {  
   @Override  
   public String eat() {  
     return "Dog is now eating";  
   }  
   public String bark(){  
     return "Dog is now barking";  
   }  
 }  

Now, we can upcast Dog(Subclass) to Animal(Superclass) as shown below:

 package typecasting;  
 public class TypeCastingMain {  
   public static void main(String[] args) {  
     Animal animal = new Dog();  
     System.out.println(animal.eat());  
   }  
 }  

Output:
 Dog is now eating  

The important point to note here is that Upcasting limits the number of properties and methods available to the child class. So in the above case, we can not call the bark() method of Dog class since it has been to upcast to Animal.class, hence only eat() method of parent class Animal is available to Dog class.

Downcasting 

When superclass object type is cast to its subclass type, it is called downcasting. For example, consider the below code:

 package typecasting;  
 public class TypeCastingMain {  
   public static void main(String[] args) {  
     Animal animal = new Dog();  
     //Downcasting  
     Dog dog = (Dog) animal;  
     System.out.println(dog.eat());  
     System.out.println(dog.bark());  
   }  
 }  

Output:
 Dog is now eating  
 Dog is now barking  

In the above code snippet, since reference variable animal is of type dog, we can explicitly cast it to Dog with the help of cast operator(the parenthesis before the animal variable).

During downcasting, there is a runtime check to make sure that the type we downcast, matches the type of the actual object. For example, the below code will not work and will throw ClassCastException at runtime.

 package typecasting;  
 public class TypeCastingMain {  
   public static void main(String[] args) {  
     Animal animal = new Animal();  
     //Downcasting  
     Dog dog = (Dog) animal;  
     System.out.println(dog.eat());  
     System.out.println(dog.bark());  
   }  
 }  

Output:
 Exception in thread "main" java.lang.ClassCastException: typecasting.Animal cannot be cast to typecasting.Dog  
      at typecasting.TypeCastingMain.main(TypeCastingMain.java:10)  

The above code will compile successfully but will throw a ClassCastException at runtime as the animal variable above is of type Animal and we are trying to downcast it to type Dog.

Conclusion:

In this blog post, we have gone through typecasting as well as widening and narrowing cast. We have also discussed how the casting occurs between object references using Upcasting and Downcasting. 

No comments:

Powered by Blogger.