0

I got confused when I saw these two techniques through which we can pass our command line argument in main method.

I have seen this link in stackoverflow but still I don't get it.

My doubt is which is efficient between these two ways.

1.In first we are calling main and assigning parameter as a string array 2.In second we are calling main with variable no of arguments.

Community
  • 1
  • 1
Satya
  • 1,011
  • 7
  • 6

1 Answers1

4

It means that if you want to invoke the main method of a class that isn't the entry point, it's easier:

class MyProgram1 {
    public static void main(String[] args) {
        MyProgram2.main(new String[] {"arg1", "arg2", "arg3"})
    }
}

vs:

class MyProgram1 {
    public static void main(String[] args) {
        MyProgram2.main("arg1", "arg2", "arg3")
    }
}
Eric
  • 95,302
  • 53
  • 242
  • 374
  • Dear @Eric Which is efficient and when we use String... args[]?? – Satya Mar 03 '14 at 00:53
  • @Satya: It's your main method. It's called once. You do __not__ care about the efficiency of that invocation. You use `String... args[]` when you want the error `legacy array notation not allowed on variable-arity parameter`. You use `String[]... args` when you want to pass a variable number of string arrays. – Eric Mar 03 '14 at 01:03