Arguments
Command-Line Arguments
Sometimes you will want to pass some information into a program when you run it.
This is accomplished by passing command-line arguments to main( )
.
A command-line argument is the information that directly follows the program’s name on the command line when it is executed.
The command-line arguments inside a Java program are stored as strings in the String
array passed to main()
.
Variable Number of Arguments
JDK 1.5 enables you to pass a variable number of arguments of the same type to a method.
The parameter in the method is declared as follows:
typeName... parameterName
In the method declaration, specify the type followed by an ellipsis (...
).
Only one variable-length parameter may be specified in a method.
This parameter must be the last parameter, and any regular parameters must precede it.