What is Typecasting in Java and How does it work?
Programming is playing around with data. In Java, there are many data types. Most of the time 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.
Below topics are covered in this article:
- What is Type Casting?
- Widening Casting
- Narrowing Casting
Let’s get started!
What is Type Casting?
Typecasting 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 cast 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 typecasting.
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 the 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);
}
}
Output:
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 a compile-time error, due to which we need to typecast 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 typecast 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 as well.
With this, we come to an end to this blog. If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.
Do look out for other articles in this series which will explain the various other aspects of Java.
1. Object Oriented Programming
6. Java Array
8. Java Threads
9. Introduction to Java Servlets
11. Exception Handling in Java
14. Java Programs
15. Kotlin vs Java
16. Dependency Injection Using Spring Boot
22. Socket Programming In Java
25. Library Management System Project in Java
26. Trees in Java
28. Top Data Structures & Algorithms in Java
30. Top 55 Servlet Interview Questions
34. Java Collections Interview Questions and Answers
35. How to Handle Deadlock in Java?
36. Top 50 Java Collections Interview Questions You Need to Know
37. What is the concept of String Pool in Java?
38. What is the difference between C, C++, and Java?
39. Palindrome in Java- How to check a number or string?
40. Top MVC Interview Questions and Answers You Need to Know
41. Top 10 Applications of Java Programming Language
42. Deadlock in Java
43. Square and Square Root in Java
Originally published at https://www.edureka.co on July 25, 2019.