Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

What happens with each call?

Java
static void log(String... args) {
System.out.println(args == null ? "null" : args.length);
}
log();
log("a", "b");
log((String) null);
log((String[]) null);

The calls print 0, 2, 1 and null. A varargs parameter is just an array that the compiler builds at the call site: log() passes an empty array, log("a", "b") passes an array of two, log((String) null) passes an array with one null element, and log((String[]) null) passes no array at all, so args itself is null. A bare log(null) also passes a null array, but javac warns because the intent is unclear.

Why this question is asked

Varargs make APIs such as String.format and List.of pleasant to call, and most of the time we never think about how they work. The edge cases with null are where that understanding pays off, because they decide whether a method receives an empty array, an array holding null, or a null array, and each needs different handling. Interviewers ask this little puzzle to see whether we know that varargs are an array in disguise and how the compiler decides what to build. In this answer, we will recap what varargs are, go through each call and what the compiler generates, look at the bare log(null) case and its warning, and write a version of the method that handles all of them safely.

1A Quick Recap: Varargs Are an Array Parameter

When we declare static void log(String... args), the method really has one parameter of type String[]. Inside the method, args is an ordinary array: we read args.length and args[i] like any other array. In the compiled class file, the method is simply log(String[]) with a flag that marks it as varargs.

The convenience is on the caller's side. When we call log("a", "b"), the compiler writes the code to create the array for us, as if we had written log(new String[] {"a", "b"}). So the question for each call is: what array, if any, does the compiler build?

2Going Through Each Call

Here is the method with all four calls, and the output of each.

Java
static void log(String... args) {
System.out.println(args == null ? "null" : args.length);
}
log(); // 0 compiler passes new String[0]
log("a", "b"); // 2 compiler passes new String[] {"a", "b"}
log((String) null); // 1 compiler passes new String[] {null}
log((String[]) null); // null the argument already is a String[], so it is passed as is
What the compiler passes for each call
Callargs inside logPrints
log()new String[0]0
log("a", "b")new String[] {"a", "b"}2
log((String) null)new String[] {null}1
log((String[]) null)null (no array)null
log(null)null (no array), with a warningnull

For log(), there are no arguments to put in the array, so the compiler creates an empty array. args is never null here, and its length is 0.

For log("a", "b"), the compiler packs both strings into a new array of length 2.

For log((String) null), the cast tells the compiler that this argument is a single String, which happens to be null. A single String does not match the parameter type String[], so the compiler treats it as one variable argument and wraps it: new String[] {null}. The array exists and has length 1; its only element is null.

For log((String[]) null), the cast says the argument is a String[]. That matches the parameter type directly, so the compiler does not build an array at all. It passes the argument as the array itself, and since that value is null, args is null and the method prints null.

3How the Compiler Decides

The rule behind these results is part of overload resolution. The compiler first tries to call the method as if it were a plain method taking a String[], without any array wrapping. Only if that fails does it try the varargs form, where it collects the arguments into a new array.

So when the single argument can already be a String[], the plain form wins and nothing is wrapped. A value cast to String[] obviously can. A value cast to String cannot, so the compiler falls back to the varargs form and wraps it. This is the reason the two casts give different results even though the value is null both times.

4The Bare log(null) Case

Without a cast, null has no type of its own; it can be assigned to any reference type, including String[]. So the plain form applies, and log(null) passes a null array, printing null like the String[] cast.

Java
log(null); // null
// javac: warning: non-varargs call of varargs method with inexact argument type for last parameter;
// cast to String for a varargs call
// cast to String[] for a non-varargs call and to suppress this warning

Notice the warning. The compiler can see that null would also fit as a single String element, so a reader could easily expect args to be an array holding null. The warning asks us to say which one we mean with a cast. In code review, we treat a bare log(null) as unclear and ask for one of the two casts.

5Writing a Method That Handles All Cases

Because a caller can pass a null array or null elements, a public varargs method should decide what each means. Often the simplest contract is to treat a null array like an empty one and to handle null elements explicitly.

Java
static void log(String... args) {
if (args == null || args.length == 0) {
System.out.println("(no arguments)");
return;
}
for (String a : args) {
System.out.println(a == null ? "<null>" : a);
}
}
log((String[]) null); // (no arguments)
log((String) null); // <null>
log("a", "b"); // a, then b

Another valid choice is to reject a null array with Objects.requireNonNull(args, "args"), which is what List.of does: List.of((String[]) null) throws NullPointerException, and List.of((String) null) also throws because List.of rejects null elements. Either way, the contract should be written down, because callers cannot guess it.

6Follow-up questions

Is the array created even if the method does nothing with it?

Yes. The compiler creates the array at the call site before the method starts, so log("a", "b") allocates a two-element array on every call even if the method returns at once. The JIT can sometimes remove the allocation after inlining, but we should not rely on it in hot code.

Can a method have two varargs parameters?

No. A method can have only one varargs parameter, and it must be the last one, because otherwise the compiler could not tell where one group of arguments ends and the next begins.

Does log() call a no-argument overload if one exists?

Yes. If both log() and log(String... args) exist, log() calls the no-argument method, because the compiler prefers a method that applies without varargs. The varargs version is only chosen when nothing else fits.

7Conclusion

In this answer, we saw that a varargs parameter is an ordinary array built by the compiler at the call site. log() receives an empty array and prints 0, log("a", "b") receives two elements and prints 2, log((String) null) receives an array holding one null and prints 1, and log((String[]) null) receives a null array and prints null. The compiler first tries to pass the argument as the array itself, which explains the difference between the two casts, and it warns on a bare log(null) because the intent is ambiguous. A public varargs method should decide and document how it treats both kinds of null.

What to say in the interviewAbout 80 seconds aloud

Open with

Varargs are just an array parameter: String... args is a String[] in the method, and the compiler builds the array at the call site. So log() gets an empty array and prints 0, and log("a", "b") gets a two-element array and prints 2.

Then, if they want more

The interesting ones are the nulls. With (String) null, I am telling the compiler the argument is one String, which doesn't match String[], so it falls back to the varargs form and wraps it, creating an array with one null element, so it prints 1.

With (String[]) null, the argument already is a String[], and the compiler always tries the plain, non-varargs form first, so it passes it directly, args is null, and it prints null.

A bare log(null) behaves the same way, passing a null array, but javac gives a warning about an inexact argument type and suggests adding a cast, because a reader could easily think it means an array containing null.

So in a real API I would decide what a null array means, either treat it as empty or reject it with requireNonNull like List.of does, and handle null elements explicitly. And I would remember that each varargs call allocates that array, even if the method returns immediately.

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.
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 "What happens with each call?": https://scrivio-interview-library.netlify.app/java/arrays/aacfdb22ea/