简体   繁体   中英

java coding confusion about String args[] and String[] args

I am new in programming with Java and I am confused about the following two statements:

public static void main(String args[])

and

public static void main(String[] args)

Are they same? If not, how are they different from each other?

两种形式之间没有语义差异,唯一的区别是风格。

They mean the same thing. The second form is generally preferred, as it puts the array declaration with the type declaration. By the way, there's nothing special about this appearing in the main() method, arrays can be declared both ways any place in your code.

While it's true for single statements there IS a difference in case you define more than one variable:

String[] foo1, foo2; // both variables are of type String[]
String bar1[], bar2; // here they're not. But you really shouldn't do this, causes
                     // unnecessary confusion

是的,它们是相同的,但惯例是将String[] args写为String[]是类型。

Both have exactly the same meaning. However, the first is unconventional and should not be used, because it splits type information. It's a holdover from C.

Its also basically the same as

public static void main(String... args)

which is what I prefer.

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