Quick revision
The short answer
m(null) prints String, because both overloads accept null and the compiler picks the most specific one, and String is a subtype of Object. p(5) prints long, because overload resolution first tries methods that need only widening, then boxing, then varargs, so widening int to long wins. m(o) prints Object, because overloads are chosen at compile time from the static type of the argument, unlike overriding, which uses the runtime type.
What to remember
In this answer, we saw that overloads are chosen at compile time from the static types of the arguments, in three phases: first widening only, then boxing, then varargs, with the most specific applicable method winning inside a phase. That is why m(null) prints String, p(5) prints long and m(o) prints Object. The phases exist so that code written before Java 5 kept calling the same methods after boxing and varargs arrived, and the practical lesson is to avoid overloads that confuse callers with null, boxing or supertypes.
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.
m(null) prints String, because both overloads accept null and the compiler picks the most specific one, and String is a subtype of Object. p(5) prints long, because overload resolution first tries methods that need only widening, then boxing, then varargs, so widening int to long wins. m(o) prints Object, because overloads are chosen at compile time from the static type of the argument, unlike overriding, which uses the runtime type.
Why this question is asked
Overloading, several methods with the same name and different parameter types, is everywhere in Java APIs, from println to assertEquals. Most of the time the compiler's choice is the obvious one, but when null, boxing, varargs or a supertype variable are involved, the chosen method can surprise us and produce a quiet bug. Interviewers ask this puzzle to see whether we know that overloads are resolved at compile time and in which order the compiler considers conversions. In this answer, we will recap the difference between overloading and overriding, walk through the three phases of overload resolution, apply them to each call, and see why the rules were designed this way.
1A Quick Recap: Overloading Versus Overriding
Overriding is when a subclass provides its own version of a method with the same signature. Which version runs is decided at runtime, by the actual class of the object. This is dynamic dispatch.
Overloading is when one class has several methods with the same name but different parameter lists. Which one is called is decided by the compiler, at compile time, from the static types of the arguments: the declared types the compiler sees in the source, not the types of the objects at runtime. Once the compiler has chosen an overload, the choice is fixed in the bytecode and never revisited.
2The Three Phases of Overload Resolution
The compiler finds the applicable methods in up to three phases and stops at the first phase that finds any. Phase 1 allows only conversions that need no boxing and no varargs, such as identity and widening primitive conversion (int to long) or widening reference conversion (String to Object). Phase 2 also allows boxing and unboxing. Phase 3 also allows variable-arity methods, the ones declared with an ellipsis.
If a phase finds several applicable methods, the compiler chooses the most specific one: a method is more specific than another if its parameter types could be passed to the other without error. If no single method is most specific, the call is ambiguous and does not compile.
- Phase 1: applicable with widening only, no boxing, no varargs?Yespick the most specific of thoseNogo to phase 2
- Phase 2: applicable with boxing or unboxing?Yespick the most specific of thoseNogo to phase 3
- Phase 3: applicable as varargs?Yespick the most specific of thoseNocompile error: no suitable method
3 steps in order. Play them, or step through them one at a time.
If a phase finds several methods and none is most specific, the call is ambiguous.
3Applying the Rules to Each Call
Now the three calls, with their output.
For m(null), the null literal can be converted to any reference type, so both m(Object) and m(String) are applicable in phase 1. The compiler chooses the most specific one. String can be passed where Object is expected, but not the other way round, so m(String) is more specific and the call prints String. If we added m(Integer), there would be two candidates, String and Integer, neither a subtype of the other, so neither is more specific and the call fails to compile with a reference to m is ambiguous error. Writing m((String) null) resolves it.
For p(5), the argument is an int. In phase 1, p(long) is applicable through widening primitive conversion, and the other two are not, because p(Integer) needs boxing and p(int...) needs varargs. Phase 1 found a method, so the compiler stops there and prints long, even though p(Integer) or the varargs version might look like a closer match at first glance.
For m(o), the variable o holds a String at runtime, but its static type is Object. The compiler only sees Object, and only m(Object) accepts an Object, so the bytecode calls m(Object). At runtime nothing changes this choice, so it prints Object. If we want behaviour that depends on the runtime type, we need overriding or a type test, not overloading.
4Why the Rules Were Designed This Way
Boxing and varargs were added in Java 5. Before that, a call like p(5) with only p(long) and p(Object) in scope already compiled and called p(long) through widening. If Java 5 had made boxing an equal choice, adding p(Integer) to a library, or simply recompiling old code against a library that had one, could have changed which method existing code called, or made it ambiguous. The phases guarantee that code that compiled before Java 5 keeps choosing exactly the same method: the new conversions are only considered when the old rules find nothing.
The practical lesson is to avoid overloads that differ only in boxed versus primitive types, or in a type and its supertype, when callers might pass null or a supertype variable. Clear method names, such as findById and findByName instead of two find methods, remove the puzzle entirely.
5Follow-up questions
How does a generic method m(T t) change the resolution?
A generic method <T> void m(T t) erases to m(Object), so it cannot coexist with m(Object) in the same class; that is a name clash at compile time. Next to m(String) alone, a call m("hi") picks m(String), because it is more specific than the generic version, while m(o) with an Object variable infers T as Object and picks the generic method. The bigger trap is inside generic code: in <T> void call(T t) { m(t); }, the compiler sees only T, whose bound is Object, so it always chooses m(Object), even when call is invoked with a String. Generics are erased, and the overload is chosen once, from the bound.
What would p(5) print if p(long) were removed?
Phase 1 would find nothing, so phase 2 allows boxing, and p(Integer) becomes applicable: it prints Integer. Only if p(Integer) were also removed would phase 3 reach the varargs version and print varargs.
Does m(null) compile if the only overloads are m(String) and m(StringBuilder)?
No. Both accept null and neither type is a subtype of the other, so there is no most specific method and the compiler reports an ambiguous reference. A cast on the null chooses one.
6Conclusion
In this answer, we saw that overloads are chosen at compile time from the static types of the arguments, in three phases: first widening only, then boxing, then varargs, with the most specific applicable method winning inside a phase. That is why m(null) prints String, p(5) prints long and m(o) prints Object. The phases exist so that code written before Java 5 kept calling the same methods after boxing and varargs arrived, and the practical lesson is to avoid overloads that confuse callers with null, boxing or supertypes.
Open with
The answer is String, long and Object, and the key idea is that overloads are chosen by the compiler from static types, unlike overriding, which is decided at runtime.
Then, if they want more
For m(null), null fits both String and Object, and the compiler picks the most specific method; String is a subtype of Object, so it prints String. If there were also an m(Integer), neither would be more specific and the call would not compile because it is ambiguous.
For p(5), resolution works in phases: first only widening is allowed, then boxing, then varargs, and it stops at the first phase that finds a match. int to long is a widening conversion, so p(long) is found in phase one and it prints long, before boxing to Integer or varargs are even considered.
For m(o), o holds a String at runtime, but its declared type is Object, so the compiler chooses m(Object) and that choice is fixed in the bytecode. The phase rules exist for backward compatibility: when Java 5 added boxing and varargs, code that already compiled had to keep calling the same method.
In practice I avoid overloads that differ only by primitive versus wrapper or by a type and its supertype, 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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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, and why?": https://scrivio-interview-library.netlify.app/java/coding-problems/27c18004dc/