Programming is playing around with data. In Java, there are many data types. Most of the times while coding, it is necessary to change the type of data to understand the processing of a variable and this is called Type Casting. In this article, I will talk about the fundamentals of Type Casting in Java.
Let’s get started!
What is Type Casting?
Type casting is nothing but assigning a value of one primitive data type to another. When you assign the value of one data type to another, you should be aware of the compatibility of the data type. If they are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion and if not, then they need to be casted or converted explicitly.
There are two types of casting in Java as follows:
Widening Casting (automatically) – This involves the conversion of a smaller data type to the larger type size.
byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually) – This involves converting a larger data type to a smaller size type.
double -> float -> long -> int -> char -> short -> byte
Now let’s get into the details of the types of type casting.
Widening Casting
This type of casting takes place when two data types are automatically converted. It is also known as Implicit Conversion. This happens when the two data types are compatible and also when we assign the value of a smaller data type to a larger data type.
For Example, The numeric data types are compatible with each other but no automatic conversion is supported from numeric type to char or boolean. Also, char and boolean are not compatible with each other. Now let’s write a logic for Implicit type casting to understand how it works.
public class Conversion{ public static void main(String[] args) { int i = 200; //automatic type conversion long l = i; //automatic type conversion float f = l; System.out.println("Int value "+i); System.out.println("Long value "+l); System.out.println("Float value "+f); } }
Int value 200 Long value 200 Float value 200.0
Now let’s move further and understand how Explicit Type Casting works.
Narrowing Casting
In this case, if you want to assign a value of larger data type to a smaller data type, you can perform Explicit type casting or narrowing. This is useful for incompatible data types where automatic conversion cannot be done.
Let’s understand this with the help of an example.
//Java program to illustrate explicit type conversion public class Narrowing { public static void main(String[] args) { double d = 200.06; //explicit type casting long l = (long)d; //explicit type casting int i = (int)l; System.out.println("Double Data type value "+d); //fractional part lost System.out.println("Long Data type value "+l); //fractional part lost System.out.println("Int Data type value "+i); } }
Output:
Double Data type value 200.06 Long Data type value 200 Int Data type value 200
Now that you know how to perform Explicit type casting, let’s move further and understand how explicit casting can be performed on Java expressions.
Explicit Type Casting in Expressions
When you are evaluating the expressions, the output is automatically updated to the larger data type of the operand. But if you store that result in any smaller data type it generates compile time error, due to which we need to type cast the output.
Example:
//Java program to illustrate type casting int to byte public class ExplicitTest { public static void main(String args[]) { byte b = 70; //type casting int to byte b = (byte)(b * 2); System.out.println(b); } }
Output:
140
Note: In case of single operands the result gets converted to int and then it is type casted accordingly.
So that was all about Explicit Type Casting in Java. With this, we come to the end of this article. I hope you found it informative. If you wish to learn more, you can check out our other Java Blogs as well.
Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.
Got a question for us? Please mention it in the comments section of this “ Type Casting in Java” article and we will get back to you as soon as possible.