Lab Core Java: Type Casting
Objective:
In this lab, we will demonstrate Java Type Casting and its types with the help of examples. Before you learn about Java Type Casting, make sure you know about Java Data Types.
Learning Objective:
By the end of this lab, the learner will be able to use Type casting in Java.
Introduction
There are two castings possible in Java:
Implicit type casting (also known as automatic type conversion).
Explicit type casting.
Implicit Type Casting
Automatic conversion (casting) done by the Java compiler internally is called implicit conversion or implicit type casting in Java. Implicit casting is performed to convert a lower data type into a higher data type. It is also known as automatic type promotion in Java.
Example: Implicit Type Casting
Output:
Explanation:
In the above example, the result of addition is double because when int and double are added, int is promoted to the higher-ranking data type than double. Therefore, assigning the result as a double is legal. When the result assigns to be an int, the fractional part of the value of y will be truncated, and the result will give the value of 60, not 60.5. It represents a loss of precision, and Java does not allow any loss of precision. Therefore, the compiler will generate an error in line number 1.
Similarly, the result of subtraction is float because when long and float are subtracted, long is promoted to the higher-ranking data type float.
Example: Automatic Type Promotion
Output:
Explanation:
In the first sub-expression, f * b, b is promoted to float and the result of sub-expression is float.
In the second sub-expression i / c, the first c is promoted to int, and the result of the subexpression will be int.
Then, in d * s, the value of s is promoted to double, and the data type of subexpression is double. Finally, we will now consider these three intermediate values with data types: float, int, and double. When the addition of a float and an int is performed, the outcome is float. Then the resultant float is minus with the last double, which is converted to double, which is the data type of the final result of the expression.
Explicit Type casting (Narrowing conversion)
The process of converting lower data types into higher data types is called Widening or Narrowing conversion in Java.
Example: Explicit Type casting
Output:
Explanation: In this example program, when the value 257 is cast into a byte variable. The output is 1, which is the remainder of the division of 257 by 256 (the range of a byte). When d is converted into an int, its fractional part is lost. When d is converted into a byte, its fractional part is also lost, and the value is reduced to module 256, which is 67 in this case.
Disadvantages of Explicit Type Casting in Java
Disadvantages of using type casting in Java:
You can lose some information or data.
Accuracy can be lost while using type casting.
When a double is cast to an int, the fractional part of a double is discarded, which causes the loss of the fractional part of data.
Last updated