BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Language basics

Java language

Varargs

A parameter declared String...

Learn these first

At a glance

Mental model

A "bring any number of guests" invitation: you hand over however many names you have, and the host collects them on one list.

What happens, in order
  1. 1Declare int sum(int..values). Calls like sum(), sum(1) and sum(1, 2, 3) all compile. An existing array can be passed directly: sum(arr).
  2. 2Only the last parameter may be varargs, and a method has at most one
  3. 3String.format, List.of, Arrays.asList and Path.of are all varargs methods
Remember
Watch out
Passing null as the only argument and getting a null arrayPassing an int[] to Object..Overloading a varargs method ambiguously
Example (highlighted lines explain it)
public class Maths {  public static int sum(int... values) {    int total = 0;    for (int v : values) total += v;    return total;  }}
Read the full explanation (about 1 min)

A parameter declared String... names lets the caller pass any number of arguments (including none). Inside the method it is just an array.

A mental model

A "bring any number of guests" invitation: you hand over however many names you have, and the host collects them on one list.

How it works

Declare int sum(int... values). Calls like sum(), sum(1) and sum(1, 2, 3) all compile. An existing array can be passed directly: sum(arr).

Only the last parameter may be varargs, and a method has at most one.

String.format, List.of, Arrays.asList and Path.of are all varargs methods.

Going deeper

Every call without an array creates a new array, which matters in hot code. The standard library therefore has fixed overloads for the first few arguments (List.of(a, b)).

Varargs with generics (T...) can cause heap pollution warnings; @SafeVarargs silences them only when the method really does not store or expose the array.

Overload resolution tries fixed-arity methods first, so adding a varargs overload next to a normal one can change which method runs.

Under the hood

The compiler rewrites the call to build an array: sum(1, 2, 3) becomes sum(new int[]{1, 2, 3}). The method's signature in the class file is a normal array parameter with a ACC_VARARGS flag.

Common mistakes

  • Passing null as the only argument and getting a null array.
  • Passing an int[] to Object... and getting one element instead of the numbers.
  • Overloading a varargs method ambiguously.

How Unravel recognises it

When you paste code, Unravel looks for these signs of varargs. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Varargs?

A parameter declared String... names lets the caller pass any number of arguments (including none). Inside the method it is just an array.

What are the most common mistakes with Varargs?

Passing null as the only argument and getting a null array. Passing an int[] to Object... and getting one element instead of the numbers. Overloading a varargs method ambiguously.

How can I see Varargs in real code?

Paste your own Java into Unravel and it will point out where varargs appears, explain how it works at the detail level you choose, and draw the flow of the code around it.