简体   繁体   中英

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?

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM