Implicit (widening) conversion
Implicit conversion, also known as widening conversion, occurs automatically when a value of a smaller data type is assigned to a variable of a larger data type. No data loss is possible because the target type can hold all values of the source type.
Widening primitive conversions
Section titled “Widening primitive conversions”The following conversions are performed automatically:
byte → short → int → long → float → double
char → int → long → float → double
Examples:
byte b = 10;short s = b; // byte → shortint i = s; // short → intlong l = i; // int → longfloat f = l; // long → floatdouble d = f; // float → doublechar to int:
char ch = 'A';int code = ch; // char → int (65)Assignment context
Section titled “Assignment context”In assignment, widening conversion is automatically applied.
int intVal = 100;long longVal = intVal; // OKdouble doubleVal = intVal; // OKArithmetic promotion
Section titled “Arithmetic promotion”In expressions, operands are promoted to a common type before the operation:
- If either operand is
double, the other is promoted todouble. - Else if either is
float, promoted tofloat. - Else if either is
long, promoted tolong. - Else both are promoted to
int.
Examples:
byte a = 10, b = 20;int c = a + b; // a and b are promoted to int, result is int
double d = a + 5.5; // a promoted to double, result doubleMethod invocation
Section titled “Method invocation”When calling a method, arguments are widened if necessary to match the parameter type.
public void print(double value) { System.out.println(value);}
print(10); // int is widened to double automaticallyBenefits
Section titled “Benefits”- Simplicity – you don’t need explicit casts for safe conversions.
- Safety – no data loss (except possible precision loss when converting
longtofloatordoubledue to floating‑point representation, but Java still considers it a widening conversion).
Note: Converting int to float or long to double is considered a widening conversion, but precision may be lost because floating‑point types have limited mantissa. However, Java still performs it implicitly.