Table of Contents
Type Casting in Java
When you are assigning a certain value to a variable, you have to make sure that both the data type of the variable as well as the value matches with each other. If not, this results in a compilation error in Java called Type Mismatch. Therefore, when you are trying to assign value of one data type to another, if the two data types are compatible with one another, via the Automatic Type Conversion, Java automatically assigns the value. However, on the odd chance that the data types are not compatible, either of the data type has to be casted to make it compatible with the other. As a simple example, if you try to assign an int value to a long variable, it will lead to a type mismatch. Therefore, you need to typecast the int value to a long value.
Widening or Automatic Type Conversion
This type of conversion happens when the two data types get converted automatically. The conditions for the same are as follows:
- When both the data types are compatible.
- Java also allows this type of conversion when a value of smaller data type is assigned to a bigger data type.
All the numeric data types are compatible with each other and get converted automatically. However, Java does not allow compatibility between numeric types to char or boolean types. Neither is char compatible with boolean. The hierarchy of the automatic conversion followed by Java is as follows:
byte => Short => Int => Long => Float => Double
When going from smaller to higher data types in the lost above, no type casting is required.
Sample Code
class Demo { public static void main (String args []) { int I = 100; long l = i; float f = l; System.out.println (“Int Value” + i); System.out.println (“Long value” + i); System.out.println (“Float Value” + i); } }
OUTPUT
Int Value 100
Long Value 100
Float Value 100.0
Narrowing or Explicit Conversion
This is the reverse process of automatic type conversion and is used when a value of higher data type has to be assigned to a variable of lower data type. As the type casting has to be done by the user, this is called explicit type casting or narrowing.
- This process comes in very handy when the two data types in question are not compatible with each other and automatic conversion cannot be carried out.
- Target-type specifies the type that you want to change your value to or convert to.
The hierarchy chart of Explicit Conversion is as follows:
Double -> Float -> Long -> Int -> Short -> Byte
As mentioned before, numerical types cannot be casted into char or boolean types neither can char be type casted into boolean or vice versa.
Sample Code
class Test { public class Test { public static void main (String args []) { char ch = ‘c’; int num = 88; ch = num; } }
OUTPUT
7: error: incompatible types: possible lossy conversion from int to char
ch = num;
^
1 error
Sample Code to demonstrate Explicit Conversion
class Demo { public static void main (String ars []) { double d = 100.04; long l = (long)d; int i = (int)i; System.out.println (“Double Value” +d); System.out.println (“Long Value” + l); System.out.println (“Int Value” + i); } }
OUTPUT
Double Value 100.04
Long Value 100
Int Value 100
Please note that when values are assigned to the byte data type, the fractional part is lost via conversion and is reduced to modulo 256 (which is the range of byte).
Sample Code
class Demo { public static void main (String args []) { byte b; int i = 257; double d = 323.142; System.out.println (“Conversion of int to byte”); b = (byte) i; System.out.println (“i = ” + i+ “b =” + b); System.out.println (“Conversion from double to byte”); b = (byte) d; System.out.println (“d =” + d + “b = ” + b); } }
OUTPUT
Conversion from int to byte
i = 257 b = 1
Conversion from double to byte
d = 323.142 b = 67
Type Promotion in Expressions
When values are evaluated, it might so happen in cases where the intermediate value crosses the range of the operands. Therefore, in such cases, the value will have to be promoted such that the range is maintained. Some of the conditions for type promotion are:
- When executing an expression with concerned data types, Java automatically promotes byte, short, or char operands to int.
- If any one of the expression values is float, double, or long, the entire expression is converted either to float, double, or long such that a bigger range can be used.
Sample Code
class Demo { public static void main (String args []) { byte b = 42; char c = ‘a’; short s = 1024; int I = 50000; float f = 5.67f; double d = 0.1234; double result = (f * b) + (I / c) – (d * s); System.out.println (“result ” + result); } }
OUTPUT
Result = 626.7784146484375
Explicit Type Casting in Expressions
As mentioned before, when expressions are evaluated, the final result is automatically converted to the highest data type (considering the data types are compatible) of the operands. However, if you try to store the result of the expression in a smaller data type, this will give you a compile time error. To be able to do that, you will have to type cast the result into the smaller data type.
class Demo { public static void main (String args []) { byte b = (byte) (b * 2); System.out.println (b); } }
OUTPUT
100
Note: When there are only single operands, the result is also converted to int by the automatic conversion of Java. Therefore, it becomes all the more important to know which type the result is and the type of the variable you want to store it in. If required, you will need to perform explicit conversion like the sample code shown above.
0 Comments