Sub topic 9
Overload Resolution: Widening, Boxing, and Varargs
Assume you know Java primitive types, reference types, ==, equals, and generic collections. This chapter focuses on how Java chooses between overloaded methods when several conversions could make a call compile.
The three stages of method selection
An overloaded method has two or more methods with the same name but different parameter lists. Java checks possible matches in stages:
| Stage | Main conversions considered | Example |
|---|---|---|
| 1. Strict invocation | Exact matches and widening, without boxing | `int` to `long` |
| 2. Loose invocation | Boxing, unboxing, and permitted reference widening | `int` to `Integer` |
| 3. Variable arity | Varargs methods such as `int...` | One or more arguments collected into an array |
Java stops at the first stage that has an applicable method. It does not continue searching for a method that seems more convenient.
Widening is considered before boxing
Widening changes a value to a type that can represent all values of the original type. For example, an int can widen to long, and an Integer can widen to Number or Object.
Boxing changes a primitive into its wrapper object, such as int to Integer.
public class OverloadResolution {
static void show(long value) {
System.out.println("long");
}
static void show(Integer value) {
System.out.println("Integer");
}
static void show(Object value) {
System.out.println("Object");
}
public static void main(String[] args) {
show(5); // long
show(Integer.valueOf(5)); // Integer
show(5.0); // Object
}
}The call show(5) can use show(long) through primitive widening. Java chooses that method before considering boxing the int into an Integer.
The second call already has static type Integer, so the Integer overload is an exact match.
For show(5.0), the argument is a double. It cannot widen to long, and it cannot narrow to int. Java can box it into a Double, then widen the reference to Object. Therefore, show(Object) is applicable in the loose-invocation stage.
A method's static argument types matter. Java does not choose an overload based on the value's apparent meaning.
Varargs are the last choice
A variable-arity parameter, written with ..., allows a method to receive zero or more values. Inside the method, the parameter is an array.
Java prefers a normal fixed-parameter method over a varargs method:
public class VarargsChoice {
static void print(int value) {
System.out.println("fixed int");
}
static void print(int... values) {
System.out.println("varargs: " + values.length);
}
public static void main(String[] args) {
print(7); // fixed int
print(); // varargs: 0
}
}print(7) matches the fixed int method during the first stage. Java does not use the varargs method even though it could place 7 into a one-element array.
print() has no fixed-parameter match, so Java uses the varargs method. The compiler creates an array equivalent to new int[0].
Ambiguity and practical rules
If two methods remain equally suitable, compilation fails. For example, overloaded methods that accept unrelated wrapper types can make null ambiguous:
static void choose(Integer value) { }
static void choose(Long value) { }
// choose(null); // compilation error: neither wrapper is more specificUse these rules when reading or designing overloads:
- Prefer exact matches and primitive widening over boxing.
- Remember that boxing can be followed by reference widening, such as
inttoIntegertoObject. - Treat varargs as a fallback.
- Avoid overload sets that differ only by unrelated wrapper types.
- Use explicit casts or factory methods when a call is unclear.
Exercise
Which method does Java call for select(3) if the overloads are select(long), select(Integer), and select(Object)?
Show answer
select(long), because primitive widening from int to long is considered before boxing.