Overloading and varargs
What you will learn
Overloading lets related methods share a name while accepting different parameter lists. Varargs let one method accept a variable number of arguments of a compatible type. Both features help callers express an intent without duplicating unrelated code.
public class Main { static String describe(int count) { return "Count: " + count; } static String describe(String label) { return "Label: " + label; } public static void main(String[] args) { System.out.println(describe(4)); System.out.println(describe("Pens")); }}Expected output:
Count: 4Label: PensThe argument type determines which overload is applicable.
How it works
For ordinary non-generic methods, the signature includes the name and ordered parameter types, not parameter names, return type or static modifier. Generic signatures have additional rules taught later. format(int) and format(String) can coexist; two format(int) declarations differing only in return type cannot. The compiler checks applicable methods in phases: strict fixed-arity invocation, loose fixed-arity invocation (including boxing), then variable-arity invocation. It chooses the most specific applicable method in the first successful phase or reports ambiguity if no unique most-specific choice exists in ordinary cases. int... values must be the last parameter and is treated as an int array inside the method. A varargs call can pass zero or more individual values or an existing int array. int... and int[] cannot be separate overloads of the same name and otherwise identical signature.
public class Main { static int sum(int... values) { int total = 0; for (int value : values) { total += value; } return total; } public static void main(String[] args) { System.out.println(sum(2, 3, 4)); System.out.println(sum()); int[] stock = {7, 8}; System.out.println(sum(stock)); }}Expected output:
9015Zero arguments produce an empty array in a variable-arity call. An existing array can also be passed directly.
Check a boundary or unusual input
public class Main { static String choose(long value) { return "long"; } static String choose(int... values) { return "varargs"; } public static void main(String[] args) { System.out.println(choose(5)); System.out.println(choose(5, 6)); }}Expected output:
longvarargsThe int argument can widen to long in the first phase. Two int arguments need the varargs overload.
Common mistakes
Do not assume the compiler chooses by return type or always chooses varargs with the closest element type. pick(int, long) and pick(long, int) can make pick(1, 1) ambiguous. A literal null can match unrelated reference overloads and be ambiguous; passing a null array to a varargs method is not the same as passing zero arguments. Avoid null varargs arrays unless explicitly supported.
Try it yourself
Implement static int countPositive(int... values) counting values greater than zero. Inputs contain at most 10 integers from -10 to 10 and are never null. Test (2, -1, 0, 3), (), (-3, 0) and (1).
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Overloading and varargs — knowledge check
Apply overloading and varargs to examples and boundary conditions.
Which pair is a valid overload?
Where must a varargs parameter appear?
How many elements does sum() receive with int... values?
Given choose(long) and choose(int...), which is selected for choose(5)?
Why can pick(1, 1) be ambiguous for pick(int,long) and pick(long,int)?