Core Java - SPLessons

Type Casting in Java

Chapter 20

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Type Casting in Java

Type Casting in Java

shape Introduction

Type Casting in Java, Converting one type to another is known as Type Casting in Java. Since Java is an Object oriented programming dialect and backings both Inheritance and Polymorphism, It’s simple that Super class reference variable is indicating SubClass object, yet the catch here is that there is no chance to get for the Java compiler to realize that a Super class variable is indicating SubClass object. Which implies user can not call a method which is pronounced on the subclass. Keeping in mind the end goal to do that, user initially need to cast the Object once more into its unique sort. This is called sort throwing in Java.

Primitive Type Casting in Java

shape Description

Type Casting in Java, Converting one data type to another is known as Primitive Type Casting in Java. It is again classified into:

shape Conceptual
figure


Casting for char and Boolean is not possible.

shape Example

package com.spl.type;

public class TypeCasting1 {
	public static void main(String[] args) {
		System.out.println("Program starts");
		int k=(int)23.95;//performing narrowing
		System.out.println("K value="+k);
		double d=(double)12;//performing widening
		System.out.println("D value="+d);
		System.out.println("Program ends");
		
	}
}

Output:
When compile the code output will be as follows.

Program starts
K value=23
D value=12.0
Program ends

shape Example

package com.spl.type;

public class Typecasting2 {
	public static void main(String[] args) {
		System.out.println("Program starts");
		Sample s=new Sample();
		s.test1(7);
		System.out.println("Program ends");
		
	}
}
class Sample
{
	void test1(double d)
	{
		System.out.println("d value="+d);
	}
	void test1(int k)
	{
		System.out.println("k value="+k);
	}
}

Output:
When compile the code result will be as follows.

Program starts
K value=7
Program ends

shape Example

package com.spl.type;

public class TypeCasting3 {
	public static void main(String[] args) {
		System.out.println("Program starts");
		Sample1 s=new Sample1();
		//explicit narrowing
		int retValue=(int)s.test1();
		System.out.println("raturn value="+retValue);
		System.out.println("Program ends");
		
	}
}
class Sample1
{
	double test1()
	{
		System.out.println("Running test1");
		return 4;//auto widening
	}
	
}

Output:
When compile the code result will be as follows.

Program starts
Running test1
return value=4
Program ends

Widening

shape Description

Type Casting in Java, Compiler first looks for same type, then goes for widening. While invoking overloaded method, the compiler gives preference to same type of argument. If it is not found then it performs widening automatically.

Derived Type Casting in Java

shape Description

Converting one type of an object to another type of object is known as Derived Type Casting in Java. The derived type casting can be done only if classes are involved in Is –A relationship.

shape Example

package com.spl.type;

public class Typecasting4 {
	public static void main(String[] args) {
		System.out.println("Program starts");
		System.out.println("Upcasting");
		C c1=new C();
		B b1=(B)c1;
		b1.test1();
		System.out.println("Auto upcasting");
		C cRef=new C();
		Sample3.run(cRef);
		System.out.println("Program ends");
		
	}
}
class A
{
	void test1()
	{
		System.out.println("Running test1()");
	}
}
class B extends A
{
	void test2()
	{
		System.out.println("Running test2()");
	}
}
class C extends B
{
	void test3()
	{
		System.out.println("Running test3()");
	}
}
class Sample3
{
	static void run(A aref)
	{
		System.out.println("Running run()");
		C cref=(C)aref;//downcasting
		cref.test1();
	}
}

The compiler compiles down costing statement, but JVM throws an exception called classcastexception. The only upcasted object contains all the behavior, but hiding the subclass features. Whenever we have to access subclass features or behavior from upcasted object, downcasting has to be performed.
Output:
When compile the code result will be as follows.

Program starts
Upcasting
Running test1()
Auto upcasting
Running run()
Running test1()
Program ends

Example of Java Runtime Polymorphism through upcasting

The following is an simple example to understand the overview of an upcasting.

class A{}  
class B extends A{}  
A a=new B();//upcasting  

In the above example, reference variable of Parent class(A) refers to the object of Child class(B) so this is known as upcasting. The following is an example by using polymorphism .

class Software{  
  void run(){System.out.println("running");}  
}  
class Facebook extends Software{  
  void run(){System.out.println("Having more than 100 million users");}  
  
  public static void main(String args[]){  
    Software b = new Facebook ();//upcasting  
    b.run();  
  }  
}  

In the above example also upcasting has been done successfully so when you compile the code result will be as follows.

Having more than 100 million users

Summary

shape Key Points

  • Type Casting in Java is the process of changing the type of data to another type.
  • Converting lower data type to higher data type is known as Widening.