In this article you will learn about Java type conversion and casting with appropriate examples. Converting a value from one type to another type (data type) is known as type conversion.
Type conversion is of two types based on how the conversion is performed: 1) Implicit conversion (also known as automatic conversion or coercion), 2) Explicit conversion (also known as type casting).
Implicit Conversion or Coercion
This type of conversion is performed automatically by Java due to performance reasons. Implicit conversion is not performed at all times.
There are two rules to be satisfied for the conversion to take place. They are:
- The source and destination types must be compatible with each other.
- The size of the destination type must be larger than the source type.
For example, Java will automatically convert a value of byte into int type in expressions since they are both compatible and int is larger than byte type.
Since a smaller range type is converted into a larger range type this conversion is also known as widening conversion. Characters can never be converted to boolean type. Both are incompatible.
Explicit Conversion or Casting
There may be situations where you want to convert a value having a type of size less than the destination type size. In such cases Java will not help you. You have do it on your own explicitly. That is why this type of conversion is known as explicit conversion or casting as the programmer does this manually.
Syntax for type casting is as shown below:
An example for type casting is shown below:
int a = 10;
byte b = (int) a;
In the above example, I am forcing an integer value to be converted into a byte type. For type casting to be carried out both the source and destination types must be compatible with each other. For example, you can’t convert an integer to boolean even if you force it.
In the above example, size of source type int is 32 bits and size of destination type byte is 8 bits. Since we are converting a source type having larger size into a destination type having less size, such conversion is known as narrowing conversion.
A type cast can have unexpected behavior. For example, if a double is converted into an int, the fraction component will be lost.
Type promotion in expressions
In addition to assignment statements, there is another place where type conversion can occur. It is in expressions. An expression is a collection of variables, values, operators and method calls which evaluate to a single value.
Type promotion rules of Java for expressions are listed below:
- All char, short and byte values are automatically promoted to int type.
- If at least one operand in an expression is a long type, then the entire expression will be promoted to long.
- If at least one operand in an expression is a float type, then the entire expression will be promoted to float.
- If at least one operand in an expression is a double type, then the entire expression will be promoted to double.
To understand the above type promotion rules let’s consider the following example of expression evaluation:
public static void main(String args)
int i = 1000000;
char c = 'z';
short s = 200;
byte b = 120;
float f = 3.45f;
double d = 1.6789;
double result = (f * b) + (i / c) - (d * s);
System.out.println("Result = "+result);
Output of the above program is: Result = 8274.22
In the above program the expression is (f * b) + (i / c) – (d * s). In the first sub expression (f * b), as one operand is float, the result of the expression will be a float. In the second sub expression (i / c), char type will be promoted to int and the result of the expression will be an int. In the third sub expression (d * s), as one operand is double, the result of the expression is a double.
So the results of the sub expressions are float, int and double. Since one of them is a double, the result of the entire expression is promoted to a double.