Type Casting in Java – Implicit and Explicit Casting

Type Casting in Java is  nothing but converting a primitive or interface or class in Java into other type. There is a rule in Java Language that classes or interface which shares the same type hierrachy only can be typecasted. If there is no relationship between then Java will throw ClassCastException. Type casting are of two types they are

  1. Implicit Casting (Widening)
  2. Explicit Casting (Narrowing)

Implicit Casting in Java / Widening / Automatic type converion

Automatic type conversion can happen if both type are compatible and target type is larger than source type.

Java Type Casting - Widening

Implicit Casting of a Primitive

No Explicit casting required for the above mentioned sequence.

public class Implicit_Casting_Example 
{
	public static void main(String args[])
	{
	    byte i = 50;
	    // No casting needed for below conversion
	    short j = i;
	    int k = j;
	    long l = k;
	    float m = l;
	    double n = m;
	    
	    System.out.println("byte value : "+i);
	    System.out.println("short value : "+j);
	    System.out.println("int value : "+k);
	    System.out.println("long value : "+l);
	    System.out.println("float value : "+m);
	    System.out.println("double value : "+n);
	}
}

Output :

byte value : 50
short value : 50
int value : 50
long value : 50
float value : 50.0
double value : 50.0

Implicit Casting of a Class Type

We all know that when we are assigning smaller type to a larger type, there is no need for a casting required. Same applies to the class type as well. Lets look into the below code.

class Parent
{
	public void disp()
	{
		System.out.println("Parent disp called");
	}
}
public class Child extends Parent
{
	public static void main(String args[])
	{
		Parent p = new Child();
		p.disp();
	}
}

Here Child class is smaller type we are assigning it to Parent class type which is larger type and hence no casting is required.

Explicit Casting in Java / Narrowing

When you are assigning a larger type to a smaller type, then Explicit Casting is required

Java Type Casting - Narrowing

public class Explicit_Casting_Example 
{
	public static void main(String args[])
	{
		double d = 75.0;
		// Explicit casting is needed for below conversion
		float f = (float) d;
		long l = (long) f;
		int i  = (int) l;
		short s = (short) i;
		byte b = (byte) s;
		
		System.out.println("double value : "+d);
		System.out.println("float value : "+f);
		System.out.println("long value : "+l);
		System.out.println("int value : "+i);
		System.out.println("short value : "+s);
		System.out.println("byte value : "+b);
	}
}

Output :

double value : 75.0
float value : 75.0
long value : 75
int value : 75
short value : 75
byte value : 75

Narrowing a Class Type

We all know that when we are assigning larger type to a smaller type, then we need to explicitly type cast it. Lets take the same above code with a slight modification

class Parent
{
	public void disp()
	{
		System.out.println("Parent disp called");
	}
}
public class Child extends Parent
{
	public void disp()
	{
		System.out.println("Child disp called");
	}
	public static void main(String args[])
	{
		Parent p = new Child();
		p.disp();
		Child c = p;
		c.disp();
	}
}

When we run the above code we will be getting exception stating “Type mismatch: cannot convert from Parent to Child”

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from Parent to Child

	at Child.main(Child.java:18)

In order to fix the code, then we need to cast it to Child class

Child c = (Child) p;

Happy Learning !! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *