The process of converting a value from one data type to another is known as type conversion in Java. Type conversion is also known as type casting in Java or simply ‘casting’.
Types of Type Conversion in Java
Java supports two main types of type conversion:
- Implicit Type Conversion (also known as Automatic Type Conversion)
- Explicit Type Conversion (also known as Manual Type Conversion or Explicit Type Casting)
Let’s understand each type of conversion one by one with the help of examples.
Implicit Type Conversion in Java
If two numeric or character primitive data types are compatible with each other, Java will perform such conversion automatically for you. This type of automatic conversion performed by the Java compiler internally is called widening primitive conversion (or commonly called implicit type casting).
Java performs this conversion automatically whenever a smaller primitive type (such as a byte, short, char, or int) is promoted or assigned to a larger compatible primitive type.
For example, when we assign an int value to a long variable, the Java compiler automatically converts the int value into a long value. Consider the following example:
int x = 20;
long y = x; // Automatic widening conversion
byte z = x; // Type mismatch: cannot convert from int to byte.
In this example, assigning the int variable x to the long variable y is allowed because both are compatible with each other. Therefore, Java supports automatic widening conversion from int to long.
However, assigning the int variable x to the byte variable z produces a compilation error because Java does not automatically perform narrowing primitive conversion.
Similarly, no automatic conversion will take place from double to byte. These kinds of conversions can be done by using a technique called explicit casting.
Automatic Type Conversion in Java
When we perform arithmetic operations with operands of different types, the Java compiler automatically converts narrower types to wider types before the operation proceeds.
Wider types are those that can represent a larger range of values or higher precision. For example, if an expression mixes an int and a double, the int is automatically widened to a double so that no precision or data is lost during the calculation.
The following figure shows the widening primitive conversions allowed by Java. Let’s understand these conversions with the help of the figure.
As shown in the figure, a byte value can be converted to short, int, long, float, or double. Similarly, a short value can be converted to int, long, float, or double.
A char value can be converted to int, long, float, or double. Likewise, an int value can be converted to long, float, or double, and so on.
These conversions are known as widening primitive conversions and are performed automatically by Java when permitted by its conversion rules.
Widening primitive conversion is generally safe because it can represent the entire range of values of the source type in the destination type. However, it does not always preserve precision.
For example, the float type can represent a wider range of values than long, but it cannot represent all long values exactly. Therefore, precision may be lost during this conversion. No explicit type casting is required for widening primitive conversion.
List of Widening Primitive Conversions Allowed in Java
The following list shows the widening primitive conversions allowed by Java:
- byte → short, int, long, float, double
- short → int, long, float, double
- char → int, long, float, double
- int → long, float, double
- long → float, double
- float → double
Note that widening conversion does not always guarantee that the original value will be preserved exactly. For example, converting a long value to float may result in a loss of precision.
Rules of Automatic Type Promotion in Java
When Java performs the numeric promotion on operands of different numeric types in an arithmetic expression, it follows these rules:
- If
byte,short,char, andintoperands are used in an arithmetic expression, Java always promotes them and evaluates the result as anint. - If a
longoperand is used in the expression, the whole expression is promoted tolong(provided nofloatordoubleis present). - If a
floatoperand is used in an expression, the whole expression is promoted tofloat(provided nodoubleis present). - If any operand is a
double, the entire expression is promoted todouble. booleanvalues cannot be converted to or from any numeric type.
Examples of Automatic Type Promotion
Let’s understand the above rules of automatic numeric promotion with the help of an example program.
Example 1: Promotion to int
If an arithmetic expression contains a byte, short or char operand, Java automatically promotes them to int before performing the arithmetic operation.
public class TypePromotionExample {
public static void main(String[] args) {
byte a = 10;
short b = 20;
char c = 30;
// All three operands are promoted to int before addition.
int result = a + b + c;
System.out.println("Result: " + result);
}
}
Output:
Result: 60
In this example program:
- We have declared three variables: a of type byte, b of type short, and c of type char.
- When Java evaluates the arithmetic expression a + b + c, it automatically promotes all three operands to int.
- Java then performs the addition using int values.
- The resulting value (60) is of type int, so we can store it in the result variable of type int.
Even though all three variables use smaller primitive data types, Java promotes them to int before performing the arithmetic operation. This is known as automatic numeric promotion in Java.
Example 2: Promotion to long
If an arithmetic expression contains a long operand and neither operand is float or double, Java performs the operation using long values.
public class TypePromotionExample {
public static void main(String[] args) {
int a = 100;
long b = 200L;
// The int operand is promoted to long.
long result = a + b;
System.out.println("Result: " + result);
}
}
Output:
Result: 300
In this example program:
- We have declared two variables: a of type int and b of type long.
- When Java evaluates the expression a + b, it automatically promotes the int operand (a) to long.
- Java then performs the addition using two long operands.
- The resulting value (300) is of type long, so we store it in the result variable of type long.
Example 3: Promotion to float
If an arithmetic expression contains a float operand and no double operand, Java performs binary numeric promotion to float (after applying the initial promotion of byte, short, and char operands to int).
public class TypePromotionExample {
public static void main(String[] args) {
int a = 10;
float b = 20.5f;
// The int operand is promoted to float.
float result = a + b;
System.out.println("Result: " + result);
}
}
Output:
Result: 30.5
In this example program:
- We have declared two variables: a of type int and b of type float.
- When Java evaluates the expression a + b, it automatically promotes the int operand (a) to float.
- Java then performs the addition using two float operands.
- The resulting value (30.5) is of type float, so we store it in the result variable of type float.
Example 4: Promotion to double
If an arithmetic expression contains a double operand, Java promotes the other numeric operand to double before performing the operation. This rule takes precedence over the promotion rules for long and float.
public class TypePromotionExample {
public static void main(String[] args) {
int a = 10;
double b = 20.5;
// The int operand is promoted to double.
double result = a + b;
System.out.println("Result: " + result);
}
}
Output:
Result: 30.5
In this example:
- We have declared two variables: a of type int and b of type double.
- When Java evaluates the expression a + b, it automatically promotes the int operand (a) to double.
- Java then performs the addition using two double operands.
- The resulting value (30.5) is of type double, so we store it in the result variable of type double.
Example 5: Automatic Numeric Promotion in Java
public class AutomaticNumericType {
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;
// Expression:
double result = (f * b) + (i / c) - (d * s);
// Result after all the promotions are performed.
System.out.println("Result = " + result);
}
}
Output:
result = 626.7784146484375
In this example program:
- In the first sub-expression
f * b, the byte value b is promoted to float, and the result of the sub-expression is float. - In the second sub-expression
i / c, the char value c is promoted to int. Both operands are int, so integer division is performed. The result of division is 515 because 50000 / 97 gives an integer quotient of 515. - In the third sub-expression
d * s, the value of s is promoted to double. The multiplication result is double. - In the final expression, there are three intermediate values of types float, int, and double.
- First, when the addition of a float and an int is performed, the int value is promoted to float, and the result is of type float.
- Next, when the resulting float value is subtracted from the double value, the float value is promoted to double. Therefore, the final expression produces a double value.
Example of Invalid Widening Type Conversion in Java
The following program demonstrates invalid automatic type conversions in Java. It shows how certain primitive data types, such as long, float, and double, cannot be converted into narrower data types automatically. We will also see how to perform these conversions using explicit type casting.
public class InvalidWideningConversion {
public static void main(String[] args) {
long l = 100L;
float f = 25.5f;
double d = 100.75;
// Invalid conversions without explicit type casting.
// int i = l; // Error: long cannot be converted to int.
// int i1 = f; // Error: float cannot be converted to int.
// float f1 = d; // Error: double cannot be converted to float.
// Valid conversions using explicit type casting.
int i = (int) l;
int i1 = (int) f;
float f1 = (float) d;
System.out.println("Long to int: " + i);
System.out.println("Float to int: " + i1);
System.out.println("Double to float: " + f1);
}
}
Output:
Long to int: 100 Float to int: 25 Double to float: 100.75
In this example, we have demonstrated three conversions that cannot be performed automatically by Java:
- long to int: Java does not automatically convert a long value into an int value because this is a narrowing primitive conversion.
- float to int: Java does not automatically convert a float value into an int value because the conversion may result in the loss of the fractional part and may also exceed the int range.
- double to float: Java does not automatically convert a double value into a float value because precision or range may be lost.
To perform these conversions, we must use explicit type casting.





