
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the differences between Widening Casting (Implicit) and Narrowing Casting (Explicit) in Java?
A Type casting in Java is used to convert objects or variables from one data type to another. When we are converting or assigning one data type to another, automatic conversion will take place (if the types are compatible and the conversion is safe).
However, if there is a risk of data loss, the conversion must be done explicitly by the programmer. Let's understand the types of Java type casting and the difference between them in this article.
Types of Type Casting in Java
Java Type Casting is classified into two types, which are:
- Widening Casting (Implicit)
- Narrowing Casting (Explicit)
Widening Casting
Widening Type Conversion can happen if both types are compatible and the target type is larger than the source type. No data will be lost in this type of conversion. It is also known as automatic type conversion.
Example 1: Widening Casting of Primitive Types
In this example, we will see widening casting of primitive types.
public class ImplicitCastingExample { public static void main(String args[]) { byte i = 40; // 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 of the above code is:
byte value : 40 short value : 40 int value : 40 long value : 40 float value : 40.0 double value : 40.0
Example 2: Widening Casting of Class Type
In the below example, Child class is the smaller type, and we are assigning it to Parent class type, which is a larger type, hence no casting is required.
class Parent { public void display() { System.out.println("Parent class display() called"); } } public class Child extends Parent { public static void main(String args[]) { Parent p = new Child(); p.display(); } }
The above code will give the following result:
Parent class display() method called
Narrowing Casting
When we are assigning a larger type to a smaller type, explicit casting is required, as there is a chance of data loss. This process is called narrowing casting.
Example 1: Narrowing a Primitive Type
The below Java program shows narrowing of primitive types:
public class ExplicitCastingExample { public static void main(String args[]) { double d = 30.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); } }
On running this code, you will get the following output:
double value : 30.0 float value : 30.0 long value : 30 int value : 30 short value : 30 byte value : 30
Example 2: Narrowing a Class Type
In the following example, although p is a reference of type Parent, it refers to an object of Child. To access Child-specific methods, we need to explicitly typecast them.
class Parent { public void display() { System.out.println("Parent class display() method called"); } } public class Child extends Parent { public void display() { System.out.println("Child class display() method called"); } public static void main(String args[]) { Parent p = new Child(); Child c = (Child) p; c.display(); } }
The output of the above Java program is given below:
Child class display() method called
Widening Casting VS Narrowing Casting
The following table compares and discusses the differences between Widening and Narrowing Casting in Java:
Widening Casting |
Narrowing Casting |
---|---|
It is also known as implicit casting or automatic type conversion |
It is also known as explicit casting or manual type conversion. |
It converts a smaller type to a larger type. |
It converts a larger type to a smaller type. |
No data loss occurs during conversion. |
It may lead to data loss during conversion. |