-
Syntax of main
I'm new to Java programming and noticed that in my reading (learning) that there appear to be 2 acceptable syntax for the 'main' statement.
Example 1:
class MyClass{
public static void main(String args[]) {
// code goes here
}
}
Example 2:
class MyClass{
public static void main(String [] args) {
// code goes here
}
}
note the positon of the [] in the two examples.
Are both forms acceptable, or does it compile and run correctly only because I am not passing any arguments to the main routine?
Thanks in advance.
-
Re: Syntax of main
(String[] args)
(String args[])
(String... helicopter)
These are all valid declarations, but String[] args is considered best practice when declaring arrays of any sort, as it is far more explicit.
-
Re: Syntax of main
The best way to know for sure is to compile and test the program with and without arguments.
If you get unexpected results, post the console contents and ask some questions.
On windows: To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.