Type Casting In Java

Yuvaraj
5 min readFeb 25, 2023

In Java, type casting is the process of converting the data type of one variable into another data type in both manual and automatic ways.

The automatic conversion is done by the compiler, and the manual conversion is performed by the programmer.

Types of Type Casting

  1. Widening Type Casting
  2. Narrowing Type Casting
Type Casting

Widening Type Casting

Converting a lower data type into a higher one is called widening type casting. It is also known as implicit casting or automatic type casting (upcasting). It is done automatically. It is safe because there is no chance to lose data.

This type of casting happens when

  • Both data types must be compatible with each other.
  • The target type must be larger than the source type.
Implicit Type Casting

For example, if you assign an integer value to a variable of type double, Java will automatically convert the integer to a double. This is because the range of values that can be stored in a double is larger than the range of values that can be stored in an integer.

Narrowing Type Casting

Converting a higher data type into a lower one is called narrowing type casting. It is also known as explicit casting or casting down. It is done manually by the programmer. If we do not perform casting, then the compiler reports a compile-time error.

This type of casting happens when

  • Both data types must be compatible with each other.
  • The target type must be smaller than the source type.
Explicit Type Casting

For example, if you assign a double value to a variable of type int, you will need to manually convert the double to an int using an explicit cast. This is because the range of values that can be stored in an int is smaller than the range of values that can be stored in a double.

To perform an explicit type cast in Java, you place the desired data type in parentheses before the variable that you want to cast. For example:

double d = 3.14;
int i = (int) d; // explicit type cast from double to int

It is important to note that explicit type casting can result in data loss if the value being cast is too large to be stored in the new data type. In such cases, the value will be truncated to fit into the new data type, which may result in unexpected behavior.

public class Demo 
{
public static void main(String arg[])
{
double d = 4.5;
int i = (int) d; // type casting

System.out.println(i);
}
}

// Output: 4
Difference between type casting and type conversion

Upcasting Vs Downcasting in Java

Upcasting in Java

Upcasting is the process of converting an object of a subclass to an object of its superclass. This is generally done implicitly by the compiler and is often used to store objects of different subclasses in a collection or array of the superclass type.

For example, if you have a collection of animals, which includes dogs, cats, and birds, you can store all these objects in an ArrayList of the Animal class, which is the superclass of the Dog, Cat, and Bird classes.

Upcasting vs Downcasting

Downcasting in Java

Downcasting is the process of converting an object of a superclass to an object of its subclass. This is an explicit operation that must be performed using the cast operator and can be risky because it may result in a ClassCastException if the object is not actually an instance of the subclass. Downcasting is often used to access methods and properties that are specific to a subclass after an object has been upcasted to its superclass.

For example, if you have an ArrayList of animals and you know that some of the objects in the list are actually dogs, you can downcast those objects to Dog objects in order to call methods that are specific to the Dog class.

class A
{
public void show1()
{
System.out.println( "Parent Class");
}
}

class B extends A
{
public void show2()
{
System.out.println( "Child Class");
}
}

class Demo
{
public static void main(String arg[])
{
// A obj = new A(); It works normally
// A obj = (A) new B(); Upcasting, you can also skip that (A)
A obj = new B(); // Upcasting
obj.show1();

B obj1 = (B) obj; // Downcasting
obj1.show2();
}
}

/* Output: Parent Class
Child Class */

Upcasting and downcasting are commonly used in polymorphism, which is the ability of objects to take on different forms. Polymorphism allows you to write code that can work with objects of different types, as long as they have a common superclass or interface.

Upcasting and downcasting are important techniques for working with polymorphic code, and they allow you to write code that is more flexible and reusable.

Difference between Upcasting & Downcasting

--

--