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 -
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| public class Conversion{public static void main(String[] args){int i = 200;//automatic type conversionlong l = i;//automatic type conversionfloat f = l;System.out.println("Int value "+i);System.out.println("Long value "+l);System.out.println("Float value "+f);}} |
1
2
3
| Int value 200Long value 200Float 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| //Java program to illustrate explicit type conversionpublic class Narrowing{public static void main(String[] args){double d = 200.06;//explicit type castinglong l = (long)d;//explicit type castingint i = (int)l;System.out.println("Double Data type value "+d);//fractional part lostSystem.out.println("Long Data type value "+l);//fractional part lostSystem.out.println("Int Data type value "+i);}} |
Output:
1
2
3
| Double Data type value 200.06Long Data type value 200Int 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.
1
2
3
4
5
6
7
8
9
10
11
| //Java program to illustrate type casting int to bytepublic class ExplicitTest {public static void main(String args[]){byte b = 70;//type casting int to byteb = (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.
0 Comments