Is there a difference between:
public void main(String args[]) { ... }
and
public void main(String[] args) { ... }
I don't believe so, but I am wondering.
String[] and String... are the same thing internally, i. e., an array of Strings. The difference is that when you use a varargs parameter ( String... ) you can call the method like: public void myMethod( String... foo ) { // do something // foo is an array (String[]) internally System.
There is no difference between the two, aside from personal preference.
String[] args means an array of sequence of characters (Strings) that are passed to the "main" function. This happens when a program is executed. Example when you execute a Java program via the command line: java MyProgram This is just a test. Therefore, the array will store: ["This", "is", "just", "a", "test"]
Semantically, they are identical. However, I'd recommend using the latter syntax (String[] args
) when declaring arrays. The former syntax is there mainly for compatibility with C syntax.
Since String[]
, as a whole, is the type of the object in Java, it's more consistent and clear not to split it up.
A similar question addresses the []
after method argument list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With