Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between main(String... s) and main(String[] s)?

Tags:

java

class Test{
    public static void main(String... s){
        System.out.println("Hello");
    }
}

class Test{
    public static void main(String[] s){
        System.out.println("Hello");
    }
}

What is the difference between above two syntax of main() declaration?

Does Java has any special need to have variable length argument?

like image 894
Mohammad Faisal Avatar asked Dec 07 '22 20:12

Mohammad Faisal


1 Answers

No difference (when you run the program from the command line, i.e. what the main method is used for). The first variant appeared after Java 5 introduced varargs.

In short, varargs allows you to pass a variable number of arguments to a method. For the method body the arguments are grouped into an array. Like Test.main("foo", "bar") instead of Test.main(new String[] {"foo", "bar"}). The compiler does the array creation for you behind the scene.

like image 93
Bozho Avatar answered Dec 10 '22 11:12

Bozho