Search code examples
javaarraysstringprogram-entry-point

What are difference in different notations of String array in main method?


I've used three types of notation while passing argument to main method in java.

public static void main(String[] args)
public static void main(String args[])
public static void main(String... args)

Can anyone tell me the difference between above? Someone has used terms packed and non-packed data for explanation of first two, what are they, and is it related to these?

I think first two is somewhat related to coding convention. Am I right?


Solution

  • There is no actual difference, the variants are due to the different ways you can define an array in java syntax.

    • The standard way to define and array

      String[] args

    • C/C++ style exist from historical reasons

      String args[]

    • Varargs style (When do you use varargs in Java?)

      String… args

    All will compile to the same bytecode. I would stick with

    public static void main(String[] args)