-1

I was converting a Groovy codebase into Java and forgot to change

public static void main(String... args) {

to

public static void main(String args[]) {

and compiled and ran the project all this time only to be surprised only now that this is legal Java 8 code.

I understand that Java 8 Varargs makes it possible for a function to have arbitrary number of arguments, "compacting them into an Array" depending on their position on the method call.

But the way functions with String... args and String[] args are called syntactically differently:

void function1 (String[] args) {}
function1({"one", "two", "three"});

void function2 (String... args) {}
function2("one", "two", "three");

So how is String... args as legal as String args[] when grabbing params from the terminal?


Edit:

azurefrog linked an answer to a different question that is great. I wanted to mention another answer where the comments provide the answer I was looking for:

Why doesn't Java's main use a variable length argument list?

Comment 1: Interesting. Judging by the other answers/comments, I guess this new main declaration syntax was added with Java 1.5. So the Java runtime determines, based on your main method declaration, whether to pass the strings directly to the main or build an array first?

Comment 2: No, it always builds an array. String... args == String[] args, as far as the called method is concerned. The parameter is an array in any case

That's the question I had. Sorry if it was asked poorly.

Community
  • 1
  • 1
sargas
  • 4,422
  • 5
  • 43
  • 62
  • 2
    Basically, because the JLS says so. See [this answer](http://stackoverflow.com/a/2201895/1361506). – azurefrog Apr 22 '16 at 21:12
  • varargs is valid in Java since 1.5 I guess. Possible duplication of http://stackoverflow.com/questions/301563/difference-fnstring-args-vs-fnstring-args – Wizard Apr 22 '16 at 21:13
  • @Wizard not a duplicate. I'm not asking what one syntax is good for versus the other. I'm asking about the underlying implementation of running Java code with the same result when you use either syntaxes. – sargas Apr 22 '16 at 21:17
  • From now on I'm calling them "vargars". Sounds cool. – davidbak Apr 22 '16 at 22:07
  • This is a very beginner level question. Maybe I asked it poorly. IF you down vote, please tell me why or edit the question. That helps a lot. Thanks. – sargas Apr 22 '16 at 22:47
  • @azurefrog Thanks a lot. – sargas Apr 22 '16 at 23:03
  • Regarding the downvotes: you're presuming this behavior is an unusual (or new) feature, neither of which is true. If you remove that presumption your question simply becomes "why can I use varargs in `main()`?", and the answer is just "because the JLS says so", which isn't a very valuable question/answer. I didn't downvote, just providing feedback. – dimo414 Apr 29 '16 at 13:42

2 Answers2

3

It's been legal since varargs were added to the language, I believe. The syntax for calling them explicitly differs, sure, but command-line arguments are passed in not by other Java code but "magically."

JLS 12.1.4 specifies this:

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String. Therefore, either of the following declarations is acceptable:

public static void main(String[] args)

public static void main(String... args)
Community
  • 1
  • 1
Louis Wasserman
  • 172,699
  • 23
  • 307
  • 375
  • I was under the impression that `javaCode arg1 arg2` would turn the parameters into one array. Thus I thought the other way to define main would be with param `String[]... args` so it could take an arbitrary number of `String[]`. But I'm starting to understand the basic difference between running Java code (after compiled) and calling Java code (from other code). – sargas Apr 22 '16 at 21:22
  • Where do you think the second dimension would come from? `String[]...` has the same type as `String[][]`. – Louis Wasserman Apr 22 '16 at 21:28
  • That what I thought. Your answer provided enough light for me to identify the flaw on my thinking. Now it makes sense. – sargas Apr 22 '16 at 21:29
0

As you already said, at compile-time all varargs... are swapped out with arrays[].

Thus the java compiler recognizes your vararg parameters and puts them into an array.

See @Louis_Wasserman 's answer for the JSL quote.

https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html --- the Java Tutorial

Wecherowski
  • 549
  • 6
  • 16