Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 different signatures for main method

Can we use those 3 dots to create an array at the place of square brackets, like we used it above?? If not then why we are using this declaration only for main??

public static void main(String[] args)

or

public static void main(String... args)
like image 628
Mayank_17 Avatar asked Apr 11 '14 17:04

Mayank_17


People also ask

Can we have 2 main methods?

From the above program, we can say that Java can have multiple main methods but with the concept of overloading. There should be only one main method with parameter as string[ ] arg.

What are the two parts of a method signature?

Two of these components comprise the method signature: the method's name and the parameter list.


1 Answers

These are not two different signatures - under the hood, it is the same signature; the difference is that the compiler uses three dots to allow invocations with variable argument lists.

Since you never invoke main directly (well, you shouldn't be, anyway) the difference shouldn't matter to you. The first form of the signature is the first thing you see in the elementary books on Java, so one should stick to square brackets to gain "instant familiarity" by the readers of your code.

like image 168
Sergey Kalinichenko Avatar answered Sep 22 '22 23:09

Sergey Kalinichenko