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.

5
  • 3
    A String... is syntactic sugar. It's actually wrapped in an String[] when it's passed into the method. Commented Mar 3, 2014 at 0:42
  • docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html Commented Mar 3, 2014 at 0:42
  • syntactic sugar?? didn't get you . Commented Mar 3, 2014 at 0:48
  • The parameter is a String array in both cases. Commented Mar 3, 2014 at 0:50
  • @Satya ie a more "eye friendly" way to write the same thing Commented Mar 3, 2014 at 0:50

1 Answer 1

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")
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Dear @Eric Which is efficient and when we use String... args[]??
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.