Using ellipsis in the main method?

If I use ellipsis in the main method, will it make any difference?

public static void main(String... args) { } 
+4
source share
4 answers

No difference. This ellipsis syntax is called varargs , whose parameter type is actually an array.

This means that there are actually three possible signatures of the actual main() method:

 public static void main(String[] args) {} public static void main(String... args) {} public static void main(String args[]) {} 
+6
source

This doesn't make any difference since the JVM turns the ellipsis (also called "varargs") into an array when it is "compiled":

 void myMethod(final X... args) 

exactly matches

 void myMethod(final X[] args) 

or (less often)

 void myMethod(final X args[]) 
+3
source

Same. Given the following class files:

 $ cat MainEllipsis.java public class MainEllipsis { public static void main(String... args) {} } $ cat MainArray.java public class MainArray { public static void main(String[] args) {} } 

After compilation ( javac MainEllipsis.java MainArray.java ) we can check the compiled signatures using javap -s <class> :

 > javap -s MainEllipsis <...snip...> public static void main(java.lang.String...); descriptor: ([Ljava/lang/String;)V 

and

 > javap -s MainArray <...snip...> public static void main(java.lang.String[]); descriptor: ([Ljava/lang/String;)V 

[Ljava/lang/String; represents the type String[] , indicating that the generated method signatures are the same.

+1
source

Besides what others have said, I think it can be useful for writing unit tests of your main () method.

0
source

Source: https://habr.com/ru/post/914772/


All Articles