Overloading with Primitive Widening
What you'll learn: How Java automatically promotes smaller primitive types to larger ones when choosing between overloaded methods.
When you call an overloaded method with a primitive argument, Java doesn't always find an exact type match. In these cases, Java will widen (promote) your argument to a larger compatible type to find a matching method signature.
How Primitive Widening Works
Java follows a specific widening hierarchy for primitives:
byte→short→int→long→float→doublechar→int→long→float→double
Think of it like pouring water from a small cup into progressively larger containers—the water (your value) fits naturally into bigger spaces without loss.
When you pass a byte argument but no method accepts byte, Java tries short, then int, and so on until it finds a match.
Example Scenario
public void process(int value) {
System.out.println("int version");
}
public void process(double value) {
System.out.println("double version");
}
// Calling code:
byte b = 10;
process(b); // Prints "int version"
Here, byte widens to int because that's the closest match. Even though double would also work, Java chooses the most specific method (the smallest widening needed).
Important Notes
- Java never narrows automatically during overload resolution (no
int→byte) - Widening happens silently and safely—no data loss for integral types
- The compiler always picks the closest type match to minimize widening
Key Takeaway: When no exact primitive type match exists, Java automatically widens your argument to the smallest compatible type among the overloaded methods, following its built-in promotion hierarchy.