Perhaps you have already noticed the unusual declarations for the main method, which has parameter args of String[] type. It is clear that args is an array of strings. The main method is just like a regular method with a parameter. You can call a regular method by passing actual parameters. Can you pass arguments to main ? Of course, yes. For example, the main method in class B is invoked by a method in A , as shown below.
A main method is just a regular method. Furthermore, you can pass arguments from the command line.
You can pass strings to a main method from the command line when you run the program. The following command line, for example, starts the program TestMain with three strings: arg0 , arg1 , and arg2 :
java TestMain arg0 arg1 arg2
arg0 , arg1 , and arg2 are strings, but they don't have to appear in double quotes on the command line. The strings are separated by a space. A string that contains a space must be enclosed in double quotes. Consider the following command line:
java TestMain "First num" alpha 53
It starts the program with three strings: "First num" and alpha , and 53 , a numeric string. Note that 53 is actually treated as a string. You can use "53" instead of 53 in the command line.
When the main method is invoked, the JVM creates an array to hold the command-line arguments and pass the array reference to args . For example, if you invoke a program with n arguments, the Java interpreter creates an array like this one:
args = new String[n];
The Java interpreter then passes args to invoke the main method.
Note
If you run the program with no strings passed, the array is created with new String[0] . In this case, the array is empty with length . args references to this empty array. Therefore, args is not null , but args.length is . |
The strings passed to the main program are stored in args , which is an array of strings. The first string is stored in args[0] , and args.length is the number of strings passed.
This example presents a program that performs binary operations on integers. The program receives three arguments: an integer followed by an operator and another integer. For example, to add two integers, use this command:
java Calculator 2 + 3
The program will display the following output:
2 + 3 = 5
Figure 8.10 shows sample runs of the program.
Here are the steps in the program:
1. | Use args.length to determine whether three arguments have been provided in the command line. If not, terminate the program using System.exit(0) . |
2. | Perform a binary arithmetic operation on the operands args[0] and args[2] using the operator specified in args[1] . |
The program is shown in Listing 8.4.
1 public class Calculator { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Check number of strings passed 5 if ( args.length != 3 ) { 6 System.out.println( 7 "Usage: java Calculator operand1 operator operand2" ); 8 System.exit( ); 9 } 10 11 // The result of the operation 12 int result = ; 13 14 // Determine the operator 15 switch ( args[ 1 ].charAt( )) { 16 case '+' : result = Integer.parseInt( args[ ] ) + 17 Integer.parseInt( args[ 2 ] ); 18 break ; 19 case '-' : result = Integer.parseInt( args[ ] ) - 20 Integer.parseInt( args[ 2 ] ); 21 break ; 22 case '*' : result = Integer.parseInt( args[ ] ) * 23 Integer.parseInt( args[ 2 ] ); 24 break ; 25 case '/' : result = Integer.parseInt( args[ ] ) / 26 Integer.parseInt( args[ 2 ] ); 27 } 28 29 // Display result 30 System.out.println( args[ ] + ' ' + args[ 1 ] + ' ' args[ 2 ] 31 + " = " + result); 32 } 33 } |
Integer.parseInt(args[0]) (line 16) converts a digital string into an integer. The string must consist of digits. If not, the program will terminate abnormally.
In the sample run, "*" had to be used instead of * for the command
java Calculator 63 "*" 40
In JDK 1.1 and above, the * symbol refers to all the files in the current directory when it is used on a command line. Therefore, in order to specify the multiplication operator, the * must be enclosed in quote marks in the command line. The following program displays all the files in the current directory when issuing the command java Test * :
public class Test { public static void main(String[] args) { for ( int i = ; i < args.length; i++) System.out.println(args[i]); } }