0

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?

Kamal Nayan
  • 1,890
  • 21
  • 34

2 Answers2

2

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)
Community
  • 1
  • 1
Haim Raman
  • 11,508
  • 6
  • 44
  • 70
0

First two are same.

Infact all three are same at base. But the third kind is called as varargs and has a special purpose which is that it can be used as an optional parameter in methods. For example if you have a method that requires parameters int x, String... y then even you call the parameter without passing String... y as parameter argument, the code will compile. More information here: Methods, Optional Parameters and/or Accepting multiple Data Types

Also check this: Java varags method param list vs. array

Community
  • 1
  • 1
Jaskaranbir Singh
  • 2,034
  • 3
  • 17
  • 33