-
Notifications
You must be signed in to change notification settings - Fork 4
Reading Command Line Arguments in Java
Ramesh Fadatare edited this page Jul 12, 2019
·
1 revision
Java programs can receive command-line arguments. They follow the name of the program when we run it.
public class CommandLineArgs {
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
}
}
Command-line arguments can be passed to the main() method. Output:
$ java com.sourcecodeexamples.CommandLineArgs 1 2 3 4 5
1
2
3
4
5
The main() method receives a string array of command-line arguments. Arrays are collections of data. An array is declared by a type followed by a pair of square brackets []. So the String[] args construct declares an array of strings. The args is a parameter to the main() method. The method then can work with parameters which are passed to it.
public static void main(String[] args)