Quick revision
The short answer
f(5) calls f(long), f(Integer.valueOf(5)) calls f(Integer), and f(s) with a short calls f(long). The compiler resolves overloads in three phases: first without boxing or varargs, then with boxing and unboxing, then with varargs, and it stops at the first phase that finds an applicable method. Widening int or short to long is allowed in phase one, so it beats boxing, and the varargs f(int...) would be chosen only if nothing else applied.
What to remember
In this answer, we saw that the compiler resolves overloads in three phases: without boxing or varargs, then with boxing and unboxing, then with varargs, choosing the most specific method in the first phase that finds one. f(5) and f(s) both pick f(long) because primitive widening is allowed in phase one, and f(Integer.valueOf(5)) picks f(Integer) as the most specific identity match. Removing methods showed phase two boxing a short into a Short, which only Object accepts, and phase three varargs as the last resort, and we saw why overloads related by widening and boxing are best avoided in our own APIs.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
f(5) calls f(long), f(Integer.valueOf(5)) calls f(Integer), and f(s) with a short calls f(long). The compiler resolves overloads in three phases: first without boxing or varargs, then with boxing and unboxing, then with varargs, and it stops at the first phase that finds an applicable method. Widening int or short to long is allowed in phase one, so it beats boxing, and the varargs f(int...) would be chosen only if nothing else applied.
Why this question is asked
Overloading looks simple until a call picks a method we did not expect. Then the result depends on rules that most developers have never read, and the surprise can be a real bug, such as a call meant for a boxed overload going to a primitive one. Interviewers use this exact set of four overloads because each call tests a different part of the rules. In this answer, we will recap overloading, go through the three phases of resolution, trace each call, then remove methods one at a time to see the later phases at work, and finish with how to keep our own APIs free of these puzzles.
1A Quick Recap of Overloading
Overloading means a class has several methods with the same name and different parameter lists. Unlike overriding, the choice is made by the compiler, at compile time, from the static types of the arguments: the types the compiler sees, not the run-time classes of the objects. Once compiled, the call is fixed to one method signature.
Two conversions matter here. Widening primitive conversion turns a smaller primitive into a larger one without loss, for example short to int to long, and int to long or double. Boxing turns a primitive into its wrapper, int into Integer, and unboxing goes the other way. A widening reference conversion, also allowed, turns a type into a supertype, such as Integer into Object.
2The Three Phases of Resolution
The Java Language Specification resolves an overloaded call in up to three phases. Phase one considers only methods that are applicable by strict or loose invocation without boxing, unboxing or varargs: identity conversions, widening primitive and widening reference conversions are allowed. If phase one finds any applicable method, resolution ends there. Phase two allows boxing and unboxing too. Phase three finally allows variable arity, varargs, methods. Within the winning phase, if several methods apply, the compiler picks the most specific one, the one whose parameter types could be passed to all the others.
- Phase 1: applicable without boxing or varargs?identity, primitive widening, reference wideningYesPick most specific, stopNoGo to phase 2
- Phase 2: applicable with boxing or unboxing?YesPick most specific, stopNoGo to phase 3
- Phase 3: applicable with varargs?YesPick most specific, stopNoCompile error: no suitable method
3 steps in order. Play them, or step through them one at a time.
Later phases are only tried if earlier ones find nothing.
The reason for this order is history. Boxing and varargs were added in Java 5, and the phases were designed so that code written before Java 5 would keep calling the same methods. A call that worked through widening in Java 1.4 must not suddenly pick a new boxed or varargs overload.
3Tracing the Three Calls
Here are the overloads with the result of each call.
| Call | f(long) | f(Integer) | f(Object) | Winner |
|---|---|---|---|---|
f(5) | phase 1 (widen) | phase 2 (box) | phase 2 (box, widen) | f(long) |
f(Integer.valueOf(5)) | phase 2 (unbox, widen) | phase 1 (identity) | phase 1 (widen ref) | f(Integer) |
| f(s), s is short | phase 1 (widen) | not applicable | phase 2 (box to Short) | f(long) |
f(int...) needs phase 3 for every call, so it never wins here.For f(5), the argument's type is int. In phase one, f(long) applies because int widens to long. f(Integer) and f(Object) would need boxing, so they are not candidates yet, and f(int...) is varargs. Only one method applies in phase one, so f(long) wins. Notice that boxing to Integer looks like a closer match to many people, but the phase order decides before closeness is ever considered.
For f(Integer.valueOf(5)), the argument is already an Integer. In phase one, f(Integer) applies by identity and f(Object) applies by widening reference conversion. f(long) would need unboxing, which is not allowed in phase one. Between Integer and Object, Integer is more specific, because an Integer can be passed to f(Object) but not the other way round, so f(Integer) wins.
For f(s), the argument is a short. Short widens to int and to long, so f(long) applies in phase one, and nothing else does. The call prints long.
4Removing Overloads to See the Other Phases
The later phases are easier to understand if we take methods away. First, remove f(long).
Notice the surprising middle case. Boxing a short produces a Short, and Short and Integer are unrelated classes, so f(Integer) does not apply at all. The compiler will not box a short into an Integer, because boxing must go to the primitive's own wrapper. f(Object) accepts the Short by reference widening, so it wins. Notice also that varargs really is the last resort: it is only reached when every other method has been removed.
5Keeping Our Own APIs Free of This
These rules are consistent, but nobody should need them to read a call. Overloads become dangerous when they have the same number of parameters and the parameter types are related by widening or boxing, such as long, Integer and Object here, because readers cannot see which one a call picks. The standard library itself has a famous trap of this kind, List.remove(int index) versus List.remove(Object o). When we design APIs, giving the methods distinct names, such as fromMillis(long) and fromValue(Integer), or using a single method with a clear parameter type, avoids the question entirely.
6Follow-up questions
What happens with f(null) for these overloads?
null can be passed to any reference type, so in phase one f(Integer) and f(Object) apply, as does f(int...), because an array is a reference type and null is a valid array value. f(long) does not, since null cannot be a primitive. Since f(int...) can also be invoked with an explicit array in phase one, the compiler compares f(Integer), f(Object) and f(int[]); Integer and int[] are unrelated, so neither is more specific and the call is ambiguous. Check your compiler's message, but expect a reference to f is ambiguous error.
Is overload resolution affected by the run-time type of the argument?
No. If we write Object o = 5; f(o);, the compiler sees Object and calls f(Object), even though the object is an Integer at run time. Only overriding looks at run-time types. This is the root of the classic equals(Order) bug, where an overload is chosen by the compiler instead of an override by the JVM.
Why does varargs come last?
Varargs methods accept almost anything with the right element type, including zero arguments, so they would make many calls ambiguous if they competed equally. Putting them last means they only act as a catch-all, and existing calls written before Java 5 kept their meaning.
7Conclusion
In this answer, we saw that the compiler resolves overloads in three phases: without boxing or varargs, then with boxing and unboxing, then with varargs, choosing the most specific method in the first phase that finds one. f(5) and f(s) both pick f(long) because primitive widening is allowed in phase one, and f(Integer.valueOf(5)) picks f(Integer) as the most specific identity match. Removing methods showed phase two boxing a short into a Short, which only Object accepts, and phase three varargs as the last resort, and we saw why overloads related by widening and boxing are best avoided in our own APIs.
Open with
The compiler picks overloads at compile time in three phases. Phase one allows only identity, primitive widening and reference widening, no boxing and no varargs. Phase two adds boxing and unboxing.
Then, if they want more
Phase three adds varargs. It stops at the first phase where something applies and picks the most specific method there. So for f of 5, the argument is an int; f of long applies in phase one by widening, while Integer and Object would need boxing, so f of long is chosen.
For f of Integer.valueOf of 5, the argument is an Integer, so f of Integer applies by identity and f of Object by reference widening in phase one; f of long would need unboxing. Integer is more specific than Object, so f of Integer wins. For the short, it widens to long in phase one, so again f of long.
The varargs f of int dots would only be chosen if nothing applied in the first two phases. A nice twist is that if I removed f of long, the short would box to a Short, which is not an Integer, so it would go to f of Object.
The phase order exists so that code from before Java 5 kept calling the same methods when boxing and varargs were added. In my own APIs I avoid overloads whose parameters differ only by widening or boxing, and use different method names instead.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 5
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Which method is called in each case?": https://scrivio-interview-library.netlify.app/java/oop-principles/03c3a8f8e8/