I am looking for a relevant portion of the Java Language Specification (JLS) which describes the behaviour when invoking a variable arity (vararg) method.
Consider the method:
public static void printVarArgs(String... args) {
System.out.println(Arrays.toString(args));
}
If I invoke the method like so:
printVarArgs();
The output will look like: []
because the omission of args
at the call site has been converted into an empty array in the printVarArgs
method.
I am looking for the point of the JLS which defines this behaviour. The closest I have found is 15.12.4.2 Evaluate Arguments, but it doesn't give this example, and I'm not sure if this case is actually covered by the formal/mathematical description.
Which part of the JLS describes the automatic creation of an empty array when a vararg is omitted?
The text of that JLS section says:
In the case you are talking about, there are
k == n - 1
formal arguments, soen, ..., ek
is an empty sequence, and that means the argument is evaluated as if it was(e1, ..., en-1, new T[]{})
.In other words, the behaviour is specified in the section you were looking at.