In Java, a "widening conversion" (also called "widening primitive conversion") refers to "converting a smaller (or narrower) primitive type to a larger (or wider) one" without data loss. This happens automatically because Java handles it safely.
Example of Widening Conversions
int myInt = 100;
long myLong = myInt; // int to long (widening)
float myFloat = myLong; // long to float (widening)
double myDouble = myFloat; // float to double (widening)
Order of Widening (smallest to largest)
byte â short â int â long â float â double
â
char
- All widening conversions are safe.
- No explicit cast is needed.
Contrast with Narrowing Conversion
- Widening: safe, automatic
- Narrowing: risky, must use a cast
double myDouble = 9.78;
int myInt = (int) myDouble; // narrowing â need explicit cast
This is when you cast up the inheritance hierarchy.