Table of Contents
Type Casting in Java
Programmers change one data type to another data type so that a function correctly processes a variable.
Understanding Type Casting in Java
- Typecasting, or type conversion, is the process of assigning a primitive data type’s value to another primitive data type.
- Typecasting is automatically performed if compatibility between the two data types exists. This type of conversion is called automatic type conversion.
- On the other hand, in the absence of compatibility between the two data types, then the conversion or casting takes place explicitly.
- Typecasting in Java is also the casting of a class or interface into another class or interface.
- Java supports both polymorphism and inheritance as it is an object- oriented programming language.
- There could be instances when a SubClass object is pointed as a SuperClass reference variable. However, a Java compiler won’t know this. So, calling a method that has been declared in the SubClass is useless.
- One such rule says that interfaces or classes belonging to a single type hierarchy can be converted or cast into one another. If objects without the same type hierarchy or parent-child relationship are converted, a compile- time error to be displayed on your screen.
- Choosing two objects that belong to the same type hierarchy. But, if the type of the object you are casting and the type of object you are casting it on are not the same, then you will get a ClassCastException.
Types of Casting in Java
1. Primitive type casting
The seven primitive data type values are Boolean, Byte, Char, Short, Int, Long, Float and Double. There are two sub-types of primitive type casting:
A. Widening casting or implicit conversion:
Widening casting involves the casting of a data type with a lower value into a data type with a higher value (Widening data type) without any loss of information. It is a big risk in the precision of the casting when considering a wider conversion between different numeric data types. Loss of small bits of information or value is possible.
B. Narrowing casting or explicit conversion:
It is the opposite of widening casting. It involves the casting of a data type with a higher value into a data type with a lower value (Narrower data type). If not handled carefully, it can lead to loss of information.
An implicit conversion doesn’t require the developer to provide inputs and is performed automatically. On the other hand, an explicit conversion is performed by the developer alone.
2. Reference type casting
If two different types of classes are associated with each other by inheritance and one of those classes is the SubClass of another, then these classes can undergo casting. It is important to ensure that the casting is in line with run-time rules as well as compile-time rules in Java.
Reference type casting is divided into two types
1. Upcasting:
Upcasing involves the conversion of an object of SubType into an object of SuperType. Java has the provision for assigning the object without requiring the addition of an explicit cast. The compiler would know what is being done and would cast the SubType value to SuperType.
2. Downcasting:
Downcasting involves the conversion of a SuperType object into a Subtype object. It is the most frequently used casting wherein it is communicated to the compiler that the value of the base object isn’t it’s own but of the SuperType object.
Upcasting | Down casting |
class Super { void Sample() { System.out.println(“method of super class”); } } public class Sub extends Super { void Sample() { System.out.println(“method of sub class”); } public static void main(String args[]) { Super obj =(Super) new Sub(); obj.Sample(); } } | class Super { void Sample() { System.out.println(“method of super class”); } } public class Sub extends Super { void Sample() { System.out.println(“method of sub class”); } public static void main(String args[]) { Super obj = new Sub(); Sub sub = (Sub) obj; sub.Sample(); } } |
Key Points:
- Type Casting is the process of changing the type of data to another
- Converting lower data type to higher data type is known as Widening.
- Converting Higher data type to lower data type is known as
- Super class reference variable is indicating Sub Class
- user initially need to cast the Object once more into its unique sort, it is called sort throwing in Java.
Add Your Heading Text Here
You May Like to Browers More


