Java Method Overloading and Overload Resolution (OCP 21)
Method overloading for the OCP Java 21 exam: what makes a valid overload, and the order Java uses to pick one, from exact match and widening through boxing to varargs, plus the most specific rule, reference types, null arguments, float vs double, and ambiguous calls.
kvi5hnuCode Sprout team member7 min readOn this page
Overloading means declaring several methods with the same name and different parameter lists in the same class. You've used overloads constantly: System.out.println has ten versions, and StringBuilder.append has thirteen. That's why you can pass almost anything to them. The exam tests two skills: spotting which declarations are legal overloads, and working out which overload a given call runs.
Declaring overloads
Overloads must differ in their parameter lists: the number of parameters, their types, or the order of the types. Everything else may differ freely, but it doesn't count:
public class Garage {
public int park(String zone) { return 1; }
int park(String zone, int hours) { return 2; }
private long park(int hours, String zone) { return 3; } // same types, different order
static void park() throws IllegalStateException { } // no parameters
}
The return types, access modifiers, static and the throws clauses are all different here, and that's fine, because each method also has a different parameter list.
These are not valid overloads, because their signatures are identical:
public class Bad {
void fly(int miles) {}
int fly(int miles) { return 1; } // DOES NOT COMPILE: only the return type differs
static void fly(int km) {} // DOES NOT COMPILE: static and the name don't count
void fly(int... miles) {} // DOES NOT COMPILE: int... is int[] underneath
}
void walk(int[] steps) {}
void walk(int... steps) {} // DOES NOT COMPILE: cannot declare both
int[] and int... compile to the same parameter type. They differ only in how they can be called: only the varargs version accepts walk(1, 2, 3), but both accept walk(new int[] {1, 2, 3}).
Generic types are erased at compile time (Chapter 9), so void f(List<String> x) and void f(List<Integer> x) clash too ("name clash: … have the same erasure"). Arrays aren't erased, so int[] and Integer[] are different types and are valid overloads.
How Java chooses: three phases
When a call matches more than one overload, the compiler picks one at compile time. It works in phases, and the first phase that finds any applicable method ends the search. This order keeps code written before Java 5 behaving the same way: boxing and varargs were added later, so they're tried last.

| Phase | Conversions allowed | Example chosen for glide(1, 2) |
|---|---|---|
| 1 | exact match, primitive widening, subtype (reference widening) | glide(int, int), then glide(long, long) |
| 2 | phase 1 plus boxing and unboxing | glide(Integer, Integer) |
| 3 | phase 2 plus varargs | glide(int...) |
Within the winning phase, Java picks the most specific applicable method: the one whose parameter types could be passed to all the others. If no single method is most specific, the call is ambiguous and doesn't compile.
Here's a complete ladder:
static String fee(int m) { return "int"; }
static String fee(long m) { return "long"; }
static String fee(Integer m) { return "Integer"; }
static String fee(Object m) { return "Object"; }
static String fee(int... m) { return "int..."; }
fee(45); // int (exact, phase 1)
fee(45L); // long (exact, phase 1)
fee((short) 30); // int (short widens to int: most specific widening)
fee('A'); // int (char widens to int; no boxing to Character)
fee(Integer.valueOf(45)); // Integer (exact reference match)
fee(4.5); // Object (no double overload: phase 2 boxes to Double, which is an Object)
fee(); // int... (only varargs accepts no arguments)
fee(10, 20); // int...
Now remove the better options and watch the call move down the ladder:
static String rate(long m) { return "long"; }
static String rate(Object m) { return "Object"; }
static String rate(long... m) { return "long..."; }
rate(45); // long: phase 1 widening beats phase 2 boxing to Integer → Object
rate(Integer.valueOf(45)); // Object: phase 1 subtype (Integer is an Object) beats unbox + widen in phase 2
rate(1, 2); // long...
The first line is the one people get wrong. Even though Object looks like it accepts anything, reaching it from an int requires boxing, so it has to wait for phase 2. Widening to long is available in phase 1, so rate(long) wins.
No double conversions
Phase 2 allows boxing, and then a reference widening of the boxed type (int → Integer → Number → Object). It doesn't allow a primitive widening followed by boxing:
static void tow(Long x) {}
tow(5); // DOES NOT COMPILE: int → long → Long is not allowed
tow(5L); // fine
The reverse direction works for unboxing, though: an Integer argument can unbox to int and then widen to long for a long parameter. That happens in phase 2.
Primitives: the most specific widening
Primitive widening follows the chain byte → short → int → long → float → double, with char → int. When several widenings are possible, the narrowest target wins:
static String scale(float f) { return "float"; }
static String scale(double d) { return "double"; }
scale(3); // float: int → float is more specific than int → double
scale(3L); // float: long widens to float!
scale(3.0); // double: a double literal can't narrow to float
long → float is a legal widening (it may lose precision, but no cast is needed), so a long argument picks a float overload over a double one. This surprises most people.
A char argument prefers char, then int, long, float, double, and only then boxing to Character. Given f(int) and f(Character), the call f('a') runs f(int).
Reference types: the most specific type
For objects, "most specific" follows the type hierarchy. A subclass is more specific than its superclass, and an implementing class is more specific than its interface:
static String label(CharSequence t) { return "CharSequence"; }
static String label(List<?> items) { return "List"; }
static String label(Object other) { return "Object"; }
label("tag"); // CharSequence (String implements it)
label(new StringBuilder()); // CharSequence
label(List.of(1, 2)); // List
label(LocalDate.EPOCH); // Object
label((Object) "tag"); // Object: the compile-time type is what counts
The choice is based on the compile-time (declared) type of the argument, not the object's run-time type. Object o = "tag"; label(o); calls label(Object).
null arguments
null matches any reference type, so Java picks the most specific one:
static void f(String s) {}
static void f(Object o) {}
f(null); // f(String): String is more specific than Object
static void g(String s) {}
static void g(StringBuilder sb) {}
g(null); // DOES NOT COMPILE: reference to g is ambiguous
String and StringBuilder are unrelated, so neither is more specific. A cast, such as g((String) null), resolves the ambiguity.
Ambiguous calls
A call fails to compile when two or more methods are applicable in the winning phase and none is more specific than all the others:
static void pay(int a, long b) {}
static void pay(long a, int b) {}
pay(1, 1); // DOES NOT COMPILE: each method is better for one argument
static void mix(Integer a, Object b) {}
static void mix(Object a, Integer b) {}
mix(1, 2); // DOES NOT COMPILE: ambiguous
static void many(long... a) {}
static void many(Integer... a) {}
many(1); // DOES NOT COMPILE: ambiguous
The declarations themselves compile. Only the call is an error.
Putting it together
static String glide(String s) { return "1"; }
static String glide(String... s) { return "2"; }
static String glide(Object o) { return "3"; }
static String glide(String s, String t) { return "4"; }
System.out.print(glide("a")); // 1: exact match
System.out.print(glide("a", "b")); // 4: exact match with two parameters
System.out.print(glide("a", "b", "c")); // 2: only varargs takes three
System.out.print(glide(1)); // 3: boxes to Integer, which is an Object
System.out.print(glide()); // 2: only varargs takes none
The output is 14232.
What's current
- Nothing in this article changed in Java 22 through 25, so the rules apply to both the Java 21 exam (1Z0-830) and the Java 25 exam (1Z0-831).
- The three overload-resolution phases (exact or widening, then boxing, then varargs) have stayed the same since Java 5.
Exam traps from this article
A complete example
The program declares overload sets for primitives, boxing, varargs, reference types and floating-point widening, and prints which overload each call selects. Running it is a good way to check your predictions. Before reading the output, cover it and write down your own answers.
import java.time.LocalDate;
import java.util.List;
public class Solution {
// A full ladder of overloads: Java picks the most specific one it can reach,
// trying exact/widening first, then boxing, then varargs.
static String fee(int minutes) { return "fee(int)"; }
static String fee(long minutes) { return "fee(long)"; }
static String fee(Integer minutes) { return "fee(Integer)"; }
static String fee(Object anything) { return "fee(Object)"; }
static String fee(int... parts) { return "fee(int...)"; }
// A smaller set, to show what happens when the better options are missing.
static String rate(long minutes) { return "rate(long)"; }
static String rate(Object anything) { return "rate(Object)"; }
static String rate(long... parts) { return "rate(long...)"; }
// Reference types: the most specific type wins.
static String label(CharSequence text) { return "label(CharSequence)"; }
static String label(List<?> items) { return "label(List)"; }
static String label(Object other) { return "label(Object)"; }
// Floating-point widening: int and long both prefer float over double.
static String scale(float f) { return "scale(float)"; }
static String scale(double d) { return "scale(double)"; }
// Overloads can differ in parameter count, order, return type, modifiers and exceptions.
static int park(String zone) { return 1; }
static int park(String zone, int hours) { return 2; }
private static long park(int hours, String zone) { return 3; }
static void park() throws IllegalStateException { }
static void show(String call, String chosen) {
System.out.printf("%-26s -> %s%n", call, chosen);
}
public static void main(String[] args) {
short s = 30;
char c = 'A';
System.out.println("== fee: all five overloads present");
show("fee(45)", fee(45));
show("fee(45L)", fee(45L));
show("fee(s) [short]", fee(s));
show("fee(c) [char]", fee(c));
show("fee(Integer.valueOf(45))", fee(Integer.valueOf(45)));
show("fee(4.5)", fee(4.5));
show("fee()", fee());
show("fee(10, 20)", fee(10, 20));
System.out.println("\n== rate: no int, no Integer");
show("rate(45)", rate(45));
show("rate(Integer.valueOf(45))", rate(Integer.valueOf(45)));
show("rate(1, 2)", rate(1, 2));
System.out.println("\n== label: most specific reference type");
show("label(\"tag\")", label("tag"));
show("label(new StringBuilder())", label(new StringBuilder()));
show("label(List.of(1, 2))", label(List.of(1, 2)));
show("label(LocalDate.EPOCH)", label(LocalDate.EPOCH));
show("label((Object) \"tag\")", label((Object) "tag"));
System.out.println("\n== scale: float beats double");
show("scale(3)", scale(3));
show("scale(3L)", scale(3L));
show("scale(3.0)", scale(3.0));
System.out.println("\n== park: count and order of parameter types");
System.out.println(park("A") + " " + park("A", 2) + " " + park(2, "A"));
}
}
Output:
== fee: all five overloads present
fee(45) -> fee(int)
fee(45L) -> fee(long)
fee(s) [short] -> fee(int)
fee(c) [char] -> fee(int)
fee(Integer.valueOf(45)) -> fee(Integer)
fee(4.5) -> fee(Object)
fee() -> fee(int...)
fee(10, 20) -> fee(int...)
== rate: no int, no Integer
rate(45) -> rate(long)
rate(Integer.valueOf(45)) -> rate(Object)
rate(1, 2) -> rate(long...)
== label: most specific reference type
label("tag") -> label(CharSequence)
label(new StringBuilder()) -> label(CharSequence)
label(List.of(1, 2)) -> label(List)
label(LocalDate.EPOCH) -> label(Object)
label((Object) "tag") -> label(Object)
== scale: float beats double
scale(3) -> scale(float)
scale(3L) -> scale(float)
scale(3.0) -> scale(double)
== park: count and order of parameter types
1 2 3
Points to notice:
fee(s)andfee(c)both choosefee(int).shortandcharwiden tointin phase 1, before boxing toShortorCharacteris ever considered.fee(4.5)reachesfee(Object)through boxing, because no overload takes adoubleor anything wider.- In the
rateset,rate(Integer.valueOf(45))picksrate(Object)(phase 1 subtype), notrate(long)(unboxing, phase 2). scale(3L)choosesfloat. Wideninglongtofloatis legal and more specific than widening todouble.
Common questions
Is overload resolution done at compile time or at run time? At compile time, from the declared types of the arguments. (Overriding is then resolved at run time for the chosen signature.)
Why does Java try boxing and varargs last? For backward compatibility. Code written before Java 5 had no boxing or varargs, and adding them mustn't change which method old code calls.
Can overloads have different return types? Yes, as long as the parameter lists differ. A different return type alone is never enough.
How does var affect overloading?
var uses the type of the initializer. var n = 5; is an int, so f(n) behaves like f(5).
Summary
- Overloaded methods share a name and differ in parameter types, count or order. Nothing else counts toward the signature.
- Java picks an overload in phases: exact match and widening, then boxing and unboxing, then varargs. The first phase with an applicable method wins.
- Within a phase, the most specific method is chosen. If there isn't one, the call is ambiguous and doesn't compile.
- Java never widens and then boxes.
long→floatis a widening, andcharprefersintoverCharacter. - Resolution uses the compile-time types of the arguments.
nullpicks the most specific reference type, or fails as ambiguous.
Practice quiz
Test yourself with the method overloading quiz: 10 exam-style questions with explanations.
Finished Chapter 5? Take the Chapter 5 review: Methods: 20 mixed questions on declarations, variables, varargs, access, static, pass-by-value, boxing and overloading.
Previous: Java Pass-by-Value, Returning Objects and Autoboxing (OCP 21)
Next: Java Inheritance and Class Modifiers: extends, Object, this and super (OCP 21)
Related reading
- Java Pass-by-Value, Returning Objects and Autoboxing (OCP 21)
- Java static Members, Static Initializers and Static Imports (OCP 21)
- Java Access Modifiers: private, Package, protected and public (OCP 21)
- Java Local and Instance Variables, Effectively Final and Varargs (OCP 21)
- Java Method Declarations: Access Modifiers, Specifiers, Return Types and Parameters (OCP 21)

More in OCP Java 21
OCP Java 21 Mock Exam 3 (1Z0-830)
The third full 50-question mock exam for the OCP Java SE 21 Developer exam (1Z0-830), weighted toward the hardest areas: streams, concurrency, generics, modules and I/O. 120 minutes, 68% to pass, with explanations.
OCP Java 21 Mock Exam 2 (1Z0-830)
The second full 50-question mock exam for the OCP Java SE 21 Developer exam (1Z0-830): new questions on every objective, with more multi-select. 120 minutes, 68% to pass, with an explanation for every answer.
OCP Java 21 Mock Exam 1 (1Z0-830)
A full 50-question mock exam for the OCP Java SE 21 Developer exam (1Z0-830), covering every objective at exam difficulty. 120 minutes, 68% to pass, with an explanation for every answer.
Quiz: Final Review (OCP 21)
Ten exam-style questions mixing the most-tested OCP Java 21 rules from the final checklist: promotion, String immutability, overloading, initialization order, collections, streams, exceptions and switch patterns, with explanations.
Discussion
Sign in to ask a question or share what you learned.