While practicing Java programming problems, there are many times when we need to convert one data type to another to achieve a certain goal. But we might face an error if we don’t follow a Java trick. That trick is known as “Type Casting in Java.”
Java Type Casting is a very simple and beginner-level concept. There are many Complex Java Problems where the use of Java Type Casting is a necessity. So, if you want to become an expert Java programmer in the future, then you must understand and grasp the type casting concept.
In this article, we will first discuss about the Type Casting and its Different Types in Java. Later, we will talk about each of the types and implement them with a sample Java Program. So, let us start our journey.
Summary Or Key Highlights:
- In Type Casting, a certain Data type Variable is converted to another Data Type Variable.
- Java Type Casting can be divided into two categories: Implicit and Explicit Type Casting.
- The Implicit Type Casting is known as the Widening Conversion in Java.
- The Explicit Type Casting is known as the Narrowing Conversion in Java.
- Type Casting can also be implemented in Java Inheritances and Java Collections.
What Is Type Casting In Java? Read Below
In Java, when we are declaring any Variable, we have to provide its Data Type before it. So, the variable can only take the values of that Data Type. If we want to convert that value to another Data Type, we can’t do that simply. For that purpose, Type Casting in Java will be needed.
Java is a Strongly Typed Programming Language, and that is the reason; Type Casting is needed there. Suppose, we want to Convert Integer Value 20 to Double Value 20.0. Now, that should be done with the help of the Type Casting Method. The concept will become easier when we deep dive into it.
Historical Evolution Of Java Type Casting:
- 1990s: During this period, the Basic Type Castings were introduced in Java 1.0 and 1.4 Versions.
- 2000s: The Generics were introduced at this time which reduced the need for Explicit Casting.
- 2010s: The modern days’ Type Interference was introduced with simple Diamond Operators (<>)
- 2020s: The process of Type Casting with Inheritance where introduced during this period.
What Is Implicit Type Casting (Widening Conversion) In Java?
The first category of the Type Casting will be the Implicit Type Casting or Widening Conversion. When any Smaller Data Type is converted to any Larger Data Type will be known as the Implicit Type Casting. This Type Casting is also called the Automatic Type Conversion.
Because, during this Type of Conversion, there is no need to intervene with the programmer. This Type of Conversion can simply be done by the Java Compiler. The Syntax is very simple and can be written without any issues. This type of Casting can be highly seen in many Java Problems.
Implicit Type Casting Data Type Flow:
Now, the Implicit Type Casting follows a certain flow. If we follow that flow, we can understand why the Implicit Type Conversion is known as the Widening Conversion. In this section, we are going to check the Implicit Type Conversion flow.
From the above image, we can see that there is a Data Type Flow we have mentioned. While doing the Implicit Type Conversion, we have to always follow this flow. One thing we should note is that the Byte is the Smallest one and Double is the Largest Data Type in the image.
As the Flow is going from Byte (Smallest) to Double (Largest) that is the reason, the Implicit Type Casting is known as the Widening Conversion because the Capacity of the Data Type is increasing here.
Features Of Implicit Type Casting:
- The Source Data Type which is being Converted should be Smaller in size than the Destination.
- During, Implicit Type Conversion, we will not find any Data Loss.
- The Implicit Type Casting is automatically handled by the Java Compiler.
How To Implement Implicit Type Casting In Java?
Now, it is time the Practically Implement Implicit Type Casting. To do that, we have to follow the certain syntax. We have to make sure that the Data Type that is being Converted should be placed after the Assignment Operator (=).
General Syntax: Target DataType VariableName = Source DataType VariableName;
public class Main {
public static void main(String[] args) {
int zap = 1102;
double one = zap; // Implicit Converting From INT To DOUBLE
System.out.println("Implicit Type Conversion:");
System.out.println("Integer Value Is: " + zap);
System.out.println("Double Value Is: " + one);
}
}
Steps Of The Program:
- At first, an Integer Variable “Zap” has been taken with the value 1102.
- Now, using the above syntax, we are converting the “Zap” Variable to a Double “One” variable.
- In the end, we will print Both Variable Values to check the changes.
Output:
What Is Explicit Type Casting (Narrowing Conversion) In Java?
Now, it is time to discuss the Explicit Type Casting or Narrowing Conversion. This is opposite of the Implicit Type Casting. Here, the Largest Data Type is converted to the Smallest Data Type, that is the reason, it is known as the Narrowing Conversion.
The Explicit Type Casting is also known as Manual Type Conversion. This Data type Conversion should be done with the help of the Programmer. The Java compiler will not automatically help to do it.
Explicit Type Casting Data Type Flow:
The Explicit Type Casting also has a Data Type Flow. And while performing Explicit Type Conversion, we have to follow that flow. If we don’t follow that flow, there will be an Error in the Program. So, let us check the Data Type Flow to understand the concept deeply.
From the above image, we can see that it is the opposite of the Implicit Type Casting Flow. Here, the Largest Double Data Type is placed at the beginning and the Smallest Byte Data Type is placed at the End. Here, the flow is going from Largest to Smallest.
As the flow is going from Largest to Smallest, the Capacity of the Data Type is decreasing. That is the reason, the Explicit Type Conversion is called a Narrowing Conversion.
Rules Of Explicit Type Casting:
- The Source Data Type which is being Converted should be Largest.
- In explicit Type Casting, we can face Precision Loss issues.
- The Explicit Type Casting is manually handled by the Java Programmer.
How To Implement Explicit Type Casting In Java?
Now, it is time to discuss the Explicit Type Casting Implementation Process. Here, the syntax is a bit more difficult than the Implicit Type Casting. Here, we have to mention the Target Data Type in Braces after the Assignment Operator (=) to easily do the conversion.
General Syntax: Target DataType VariableName = (Target DataType) Source DataType VariableName;
public class Main {
public static void main(String[] args) {
double one = 1102.25;
int zap = (int) one; // Explicit Converting From DOUBLE To Int
System.out.println("Explicit Type Conversion:");
System.out.println("Double Value Is: " + one);
System.out.println("Integer Value Is: " + zap);
}
}
Steps Of The Program:
- At first, we have declared one Double Variable “One” with the value 1102.25.
- Now, using the above syntax, we are converting the Double to Integer “Zap” Variable.
- In the end, we are printing both the Variables to check the changes.
Output:
Difference Table Between Implicit And Explicit Type Casting:
We hope whatever we have discussed about the Implicit and Explicit Type Conversion has become clear to you. Now, we can discuss some Differences between the Implicit and Explicit Type Conversion. For that purpose, we have made one Difference Table.
Let us check the following difference table to have a solid understanding of the topic.
Categories | Implicit Type Casting | Explicit Type Casting |
Data Type Flow | Smaller To Larger | Larger To Smaller |
Performed By | Compiler | Programmer |
Performance Type | Automatic | Manual |
Risk Of Data Loss | No Risk | Risk Involved |
Use Type Safety | It is Safe due to Conversion between Primitive Data Types | It is a Risky Conversion |
What Are Different Types Of Casting In Objects (Reference Type Casting)?
Now, Type Casting can be seen in the Java Inheritance as well which is known as the Reference Type Casting. The Type Casting in Objects can be divided into two categories. Let us check those categories to understand the concept easily. Let us have a look at the following list.
- Upcasting: Upcasting is also known as the Type Promotion. This is Implicit Category Casting. Here, the Subclass or Child Object gets the Superclass or Parent Object Reference. This is Type Casting what we usually do while working with Inheritance.
- Downcasting: Downcasting is the opposite of the Upcasting. It is known as the Type Demotion and belongs to the Explicit Category. Here, a Child or Subclass Object which has earlier the Superclass or Parent Class Reference is getting back again the Subclass Object Reference.
The concepts will become very clear when we will discuss them with a Practical Example.
class ZapOne { // Implementing The Parent Class
void dis() {
System.out.println("Parent Class 'ZapOne' Is Called");
}
}
class CodingZap extends ZapOne { // Implementing The Child Class
void shw() {
System.out.println("Child Class 'CodingZap' Is Called");
}
}
public class Main {
public static void main(String[] args) {
ZapOne sg = new CodingZap(); // We Are Doing Upcasting (Implicit casting)
sg.dis(); // Calling Parent Function
CodingZap gs = (CodingZap) sg; // We Are Doing Downcasting (Explicit casting)
gs.shw(); // Calling Child Function
}
}
Steps Of The Program:
- Here, a Parent Class “ZapOne” is created with a function Dis() which prints a Statement.
- Then, the Child Class “CodingZap” will be created which will also have a function Shw() which also prints a statement.
- Now, we will first do the Upcasting by providing the Parent Class “ZapOne” Reference to the Child Class “CodingZap”. Using that reference, we will access the Parent Class Function Dis().
- Here, the “CodingZap” has now the Parent Class Reference. Now, we will do the Downcasting. We will provide the Child Class Reference to “CodingZap” and forcefully bring it down to the Lower Casting.
- Now, with the latest Child Class Reference, we will access the Child Class Function Shw().
Output:
How To Do Type Casting With Java Collections?
Now, we hope that you are aware of the Java Collections Concept. In Java Collections, we can work with the Array Lists and Linked Lists. We can also do Implicit Type Casting and Explicit Type Casting in Java Collections. However, the use of Type Casting in Java Collection is very rare.
Let us have a practical example to demonstrate the use of Type Casting in Java Collections.
Import java.util.*;
public class Main {
public static void main(String[] args) {
List zap = new ArrayList<>(); // Creating Java Collection
// Inserting Integer Values In The Collection
zap.add(12);
zap.add(2);
zap.add(25);
Object one = zap; // We Are Doing Implicit Casting
List sg = (List) one; // We Are Doing Explicit Casting
System.out.println(“Type Casting With Java Collections:”);
for (Integer n : sg) { // Printing Every Element From The Object
System.out.print(n + “ “);
}
}
}
Steps Of The Program:
- At first, the ArrayList “Zap” has been implemented which will only take Integer Values.
- Now, we will provide 3 Integer Values with the help of the Add() Function.
- Now, we will create an Object of ArrayList where we are doing Implicit Casting. You can understand it by analyzing the syntax which is similar to the Implicit Type Casting.
- Now, again the Implicit Type Casted Object “One” will be converted to List using the Explicit Type Conversion Method.
- In the end, we are printing the values of the newly converted List “Sg”.
Output:
What Are Some Performance Implications Of Java Type Casting?
We hope, that whatever we have discussed till now, should be enough to clear your understanding about Java Type Casting. But if you are thinking all is great with Java Type Casting, then you are thinking wrong. While working with Type Casting, we have to keep in mind some Performance Implications.
In this section, we are going to discuss some Performance Implications that can affect the Java Program execution. Let us check the following list to know more.
- If we do Unnecessary Object Casting in Java, it will increase the Memory Consumption.
- If we perform Type Casting, there will be a Minor Increase in Java Runtime Overhead.
- While Converting Primitive Data Types to Wrapper Class, the Performance Cost might get increased.
- If we perform Excessive Type Casting, the Optimization of the Java Compiler might get damaged.
- If we did not do proper casting, the Program Execution might get stopped with a ClassCastException Error.
Conclusion:
In the end, we can say, it is very necessary to know about “Type Casting in Java”.
However, we will advise you to clear the basics of Java Programming first. Then, you can start learning about Java Type Casting. If the Java Basics, especially the Java Syntax Rules are clear to you, then grabbing simple concepts like Type Casting will become a cakewalk.
Takeaways:
- Implicit Type Casting can be done for Smaller to Larger Data Type Conversion.
- Explicit Type Casting can be done for Larger to Smaller Data Type Conversion.
- The Risk of Data Loss is involved in Explicit Type Casting, not in Implicit Type Casting.
- The Upcasting and Downcasting can be seen while working with Java Inheritance.
- We can perform both Implicit and Explicit Casting with Java ArrayList and LinkedList.





