← Back to Core Java Course | Chapter 5: Methods & Arrays | Lesson 6 of 10

Java Variable Arguments (varargs)

Introduction to Varargs

Varargs (variable arguments) let a method accept any number of arguments of the same type without the caller needing to build an array first, using the Type... name syntax. Internally, Java collects whatever arguments you pass into a regular array before the method body runs.

Example: Introduction to Varargs

java
public class Main {
	static int sum(int... numbers) {
		int total = 0;
		for (int n : numbers) {
			total += n;
		}
		return total;
	}
	public static void main(String[] args) {
		System.out.println(sum(1, 2, 3));
	}
}

Varargs Syntax Rules

A method can declare at most one varargs parameter, and it must be the last parameter in the signature, because the compiler needs to know unambiguously where the fixed parameters end and the variable-length group begins.

Example: Varargs Syntax Rules

java
public class Main {
	static void printAll(String label, int... numbers) {
		System.out.print(label + ": ");
		for (int n : numbers) {
			System.out.print(n + " ");
		}
	}
	public static void main(String[] args) {
		printAll("Scores", 90, 85, 77);
	}
}

Varargs as Arrays

Since the varargs parameter really is just an array under the hood, you can pass an existing array directly instead of listing individual values, and the method behaves identically either way. This is handy when the values already come from another array or a collection converted to one.

Example: Varargs as Arrays

java
public class Main {
	static int sum(int... numbers) {
		int total = 0;
		for (int n : numbers) {
			total += n;
		}
		return total;
	}
	public static void main(String[] args) {
		int[] values = {4, 5, 6};
		System.out.println(sum(values));
	}
}

Overloading Varargs Methods

You can overload a varargs method with fixed-arity versions of the same name, but the compiler prefers the most specific matching signature, so ambiguous overloads (e.g. two versions that could both match a given call) will fail to compile rather than silently picking one.

Example: Overloading Varargs Methods

java
public class Main {
	static int sum(int a, int b) {
		return a + b;
	}
	static int sum(int... numbers) {
		int total = 0;
		for (int n : numbers) {
			total += n;
		}
		return total;
	}
	public static void main(String[] args) {
		System.out.println(sum(1, 2)); // matches the fixed-arity version
		System.out.println(sum(1, 2, 3));
	}
}

Empty Varargs

Calling a varargs method with no arguments at all is legal; the parameter still exists inside the method as an array, just with a length of 0. Code that assumes the array always has at least one element will throw an ArrayIndexOutOfBoundsException in that case, so guard against an empty array explicitly.

Example: Empty Varargs

java
public class Main {
	static int sum(int... numbers) {
		System.out.println("Length: " + numbers.length);
		int total = 0;
		for (int n : numbers) {
			total += n;
		}
		return total;
	}
	public static void main(String[] args) {
		System.out.println(sum());
	}
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.