Skip to content
Codewilds

Lesson 10 of 34 · Methods

Java Method Overloading and Varargs

medium20 XP3 min read

You have already used overloading without noticing: System.out.println accepts a String, an int, a double, a char… That is not one magic method but many methods that share a name. In this lesson you will write your own overloaded methods and learn how the compiler decides which one to call.

Same name, different parameters

Two methods in the same class may have the same name as long as their parameter lists differ — in the number of parameters, their types, or their order:

static int max(int a, int b) {
    return a > b ? a : b;
}

static int max(int a, int b, int c) {
    return max(max(a, b), c);   // reuse the two-argument version
}

static double max(double a, double b) {
    return a > b ? a : b;
}

The name plus the parameter types form the method's signature. Overloading keeps your API tidy: callers remember one name, max, instead of maxOfTwoInts, maxOfThreeInts and maxOfTwoDoubles.

How Java picks an overload

The compiler chooses the overload at compile time by looking at the argument types:

max(3, 7)       // max(int, int)         → 7
max(3, 7, 5)    // max(int, int, int)    → 7
max(2.5, 1.5)   // max(double, double)   → 2.5
max(3, 2.5)     // no max(int, double): 3 is widened to 3.0 → 3.0

An exact match wins. If there is none, Java may widen a value to a bigger type (int → long → double) and try again. If two overloads fit equally well, the call is rejected as ambiguous and you must make the types explicit.

The return type is not enough

Overloads must differ in their parameters. Two methods that differ only in their return type cannot coexist, because a call like parse("42") would give the compiler no way to choose:

static int parse(String text) { ... }
static double parse(String text) { ... }   // error: method parse(String) is already defined

Varargs: any number of arguments

Sometimes you do not know how many arguments a caller will pass. A varargs parameter, written with three dots, accepts zero or more values:

static int sum(int... numbers) {
    int total = 0;
    for (int n : numbers) {
        total += n;
    }
    return total;
}
sum()            // 0
sum(4)           // 4
sum(1, 2, 3, 4)  // 10

Inside the method, numbers holds all the values that were passed — technically it is an array, which Chapter 3 covers in depth. numbers.length tells you how many there are, and for (int n : numbers) visits each one. A varargs parameter must be the last parameter, and a method can have only one. System.out.printf is a famous varargs method.

Common mistakes

  • Overloading by return type only — not allowed; change the parameters.
  • Ambiguous calls: with f(int, double) and f(double, int), the call f(1, 2) matches both and does not compile.
  • Expecting the choice at run time: the overload is fixed by the compiler from the declared types of the arguments.
  • Putting varargs first: sum(int... numbers, String label) does not compile; varargs go last.

Assignment

Write these overloaded methods in Main:

  1. max(int a, int b) — the larger of two ints.
  2. max(int a, int b, int c) — the largest of three ints (reuse the two-argument version).
  3. max(double a, double b) — the larger of two doubles.
  4. Three describe overloads that label their argument: describe(42) returns "int: 42", describe(2.5) returns "double: 2.5" and describe("hi") returns "String: hi".
  5. sum(int... numbers) — the total of all arguments; sum() returns 0.

Premium lesson

Unlock the full Learn Java course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Function parameters in other languages

Need a quick reminder? The Java cheat sheet has every topic of Learn Java on one page.

Lesson last updated on .

Write code ↓
Java+20 XP

Lesson 10 is part of the full course. The first 8 lessons are free.

Premium lesson

Unlock the full Learn Java course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.