3.23. Program Arguments

Any arguments passed to the program on the command line can be accessed in the main() method of the class specified on the command line:

java Colors red green blue

These arguments are called program arguments. Note that the command name, java, and the class name Colors are not passed to the main() method of the class Colors.

Since the formal parameter of the main() method is an array of String objects, individual String elements in the array can be accessed by using the [] operator.

In Example 3.8, the three arguments "red", "green", and "blue" can be accessed in the main() method of the Colors class as args[0], args[1], and args[2], respectively. The total number of arguments is given by the field length of the String ...

Get Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.