Quick revision
The short answer
Varargs are a good fit when a method naturally takes zero or more values of the same type, such as String.format or EnumSet.of. The pitfalls are that every call allocates an array, which is why List.of and EnumSet.of add fixed-arity overloads for small counts; that overload resolution prefers fixed-arity methods, which can surprise callers when overloads are mixed; that requiring at least one value is better written as (T first, T... rest); and that generic varargs cause heap pollution warnings, where @SafeVarargs is only honest if the method does not store or expose the array.
What to remember
In this answer, we saw that varargs suit methods taking zero or more values of the same type, like String.format, List.of and EnumSet.of. Their costs are an array allocation on every call, which List.of and EnumSet.of avoid with fixed-arity overloads for small counts; overload resolution that tries fixed-arity methods, boxing and widening before varargs, which can surprise callers; a run-time check for at least one argument that (T first, T... rest) moves to compile time; and heap pollution for generic varargs, where @SafeVarargs is only correct if the array is never stored or exposed.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Varargs are a good fit when a method naturally takes zero or more values of the same type, such as String.format or EnumSet.of. The pitfalls are that every call allocates an array, which is why List.of and EnumSet.of add fixed-arity overloads for small counts; that overload resolution prefers fixed-arity methods, which can surprise callers when overloads are mixed; that requiring at least one value is better written as (T first, T... rest); and that generic varargs cause heap pollution warnings, where @SafeVarargs is only honest if the method does not store or expose the array.
Why this question is asked
Varargs feel like a free convenience when we write an API, but once a public method ships, its varargs shape is hard to change and its costs are paid by every caller. Interviewers ask this design question to see whether we understand both sides: the readability we gain and the allocation, overload and type safety issues we take on. In this answer, we will recap how varargs work, see where they fit, look at the allocation cost and the fixed-arity overloads in List.of, go through the overload resolution surprises, show the (T first, T... rest) pattern, and finish with heap pollution and @SafeVarargs.
1A Quick Recap of Varargs
A varargs parameter, written T... values, is an array parameter whose array the compiler builds for us at each call site. Calling of(a, b, c) compiles to of(new T[] {a, b, c}). Inside the method it is a normal array, and a caller can also pass an existing array directly.
That is all varargs are. Every design question below follows from two facts: there is an array, and the compiler decides when to build it.
2Where Varargs Fit Well
Varargs fit methods that take a list of values of the same type, where the number really varies and zero or more is meaningful. String.format("%s has %d items", name, count) takes any number of values to substitute. EnumSet.of(Day.MON, Day.WED) and List.of("a", "b", "c") build a collection from whatever we pass. Path.of("var", "log", "app") joins any number of name parts.
They fit badly when the arguments mean different things. A method like connect(String... parts) where parts[0] is the host and parts[1] the port hides the contract from the type system. Named parameters, a small record or a builder communicate that much better.
3The Allocation Cost and Why List.of Has Fixed-Arity Overloads
Because the compiler builds an array at every call, a varargs method allocates on every call, even with one argument. For most code that is irrelevant. For methods called millions of times per second, such as collection factories, logging or metrics, it adds garbage and GC work.
This is why List.of has separate overloads for zero to ten elements plus a varargs overload for more, and EnumSet.of has overloads for one to five elements plus (E first, E... rest).
Notice that the small cases, which are by far the most common, avoid the array entirely, and the List implementation can store the one or two elements in plain fields. The fixed-arity overloads are an optimisation the caller never sees, because the call looks the same either way.
4Overload Resolution Surprises
The compiler picks an overload in phases. First it looks for a method that fits without boxing and without varargs. If none fits, it allows boxing and unboxing. Only if that also fails does it consider varargs methods. So a fixed-arity overload always wins over a varargs overload when both can apply, and mixing them can make calls go somewhere the caller did not expect.
- Phase 1: fits with no boxing and no varargs?YesChoose the most specific of thoseNoGo to phase 2
- Phase 2: fits if boxing and unboxing are allowed?YesChoose the most specific of thoseNoGo to phase 3
- Phase 3: fits as a varargs call?YesBuild the array and call itNoCompile error
3 steps in order. Play them, or step through them one at a time.
Varargs methods are tried last, so a fixed-arity overload wins whenever it can apply.
The first call surprises many people: a caller who passes one int expects the int... method, but print(Object) is found earlier because boxing is tried before varargs. The last call shows the same effect with widening. Worse, adding a new fixed-arity overload in a later library version can silently redirect existing calls when callers recompile. So in an API we avoid overloading a varargs method with methods whose parameters could also accept the same arguments, or we give them different names.
5Requiring At Least One Argument
Some methods need at least one value. With a plain varargs parameter, a caller can pass nothing, and we can only fail at run time.
The (first, rest) shape moves a run-time error to compile time and documents the rule in the signature. EnumSet.of(E first, E... rest) uses exactly this pattern.
6Generic Varargs, Heap Pollution and @SafeVarargs
A generic varargs parameter such as T... has a problem: Java cannot create an array of a generic type, because generics are erased at run time. So for a call like asList(listA, listB) with List<String> elements, the compiler actually creates a List[], and it warns about possible heap pollution: a variable of a generic array type might end up holding something of the wrong type, causing a ClassCastException later in code that never cast anything.
Inside twice, T is unknown, so the array the compiler builds is an Object[]. Returning it to a caller that expects String[] fails. @SafeVarargs tells the compiler and the callers that our method is safe and silences the warning at every call site. It is only honest when the method neither stores the array nor lets it escape, and only reads its elements. It can be used on static, final and private methods and on constructors, since those cannot be overridden by an unsafe version.
7Follow-up questions
Should a public API ever accept both an array and varargs overload?
No, because they would have the same descriptor: T... and T[] compile to the same parameter type, so the compiler rejects declaring both. A varargs method already accepts an existing array, so one method serves both styles of call.
Do fixed-arity overloads break callers if added later?
Not in binary terms: already-compiled callers keep calling the varargs method they linked to. When callers recompile, calls with a matching count move to the new overload, so the new overload must behave exactly like the varargs one for the same arguments.
Is the varargs allocation always real?
Not always. After inlining, the JIT's escape analysis can sometimes remove an array that never escapes. But it depends on inlining decisions and code size, so for hot library methods we rely on fixed-arity overloads, not on the JIT.
8Conclusion
In this answer, we saw that varargs suit methods taking zero or more values of the same type, like String.format, List.of and EnumSet.of. Their costs are an array allocation on every call, which List.of and EnumSet.of avoid with fixed-arity overloads for small counts; overload resolution that tries fixed-arity methods, boxing and widening before varargs, which can surprise callers; a run-time check for at least one argument that (T first, T... rest) moves to compile time; and heap pollution for generic varargs, where @SafeVarargs is only correct if the array is never stored or exposed.
Open with
I use varargs when a method genuinely takes zero or more values of the same type, like String.format, Path.of, EnumSet.of or List.of. When the arguments mean different things, I'd use named parameters or a builder instead.
Then, if they want more
The first pitfall is cost: varargs is an array the compiler creates at every call site, so every call allocates, even with one argument.
That is why List.of has fixed-arity overloads for zero to ten elements and EnumSet.of has them for one to five, and only fall back to varargs for bigger counts; the common small calls never create an array. The second pitfall is overload resolution.
The compiler first tries methods without boxing or varargs, then with boxing, and only then varargs, so print(5) with print(Object) and print(int...) goes to the Object version, and adding a fixed-arity overload later can redirect calls when callers recompile. So I avoid mixing varargs with overlapping overloads.
If a method needs at least one value, I write it as (T first, T... rest), which is enforced at compile time. And for generic varargs, the compiler warns about heap pollution because it can't create a real T array. I only add @SafeVarargs when the method just reads the elements and never stores or returns the array.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "When designing a public API, when is varargs a good choice, and what pitfalls do...": https://scrivio-interview-library.netlify.app/java/arrays/b17d167f4b/