Witscale Test Center

8.2 The main() method > 8.2.1 Arguments to the main method


8.2.1 Arguments to the main method

The only argument the main method takes is an array of strings. When JVM calls the main method, this array contains the command line arguments (if any) passed to the java application. For instance if you have a java class which does arithmetic operations, say Calculator, you may run it as an application if you have the main method in it. You are likely to pass the operands and operations as the command line argument. For example,

 

 $ java Calculator 12 34 +

 

In the above example,  12, 34 and + are the three arguments passed to the main method of Calculator class. Figure 8.1 illustrates how these command line arguments are stored in the string array during an execution of Calculator application.


 

Figure 8.1 Command line arguments to the main method.

 

When the Calculator application is executed as shown in figure, its argument array will have “12”, “34” and “+” stored at 0th, 1st and 2nd index respectively. You can access these command-line arguments within the main method of Calculator class as:

 

public class Calculator {

 public static void main(String[] args) {

   System.out.println(args[0]);           

   System.out.println(args[1]);

   System.out.println(args[2]);

 }

}

 

Depending on what arguments are passed each time Calculator is executed, above code will produce different output. The length of the string array args is always equal to the number arguments (args.length-1) you pass when you run the Calculator.  Like all arrays, it stores the command line arguments from index 0 onwards. You will get an exception in main method if you try to access args with index greater than (args.length –1). If your program accepts different number of arguments each time it runs, you should read them in the for loop as:

 

public class Calculator {

 public static void main(String[] args) {

   for(int i=0 ; i < args.length; i++)

     System.out.println(args[i]);

 }

 

Other than the fact that it is (automatically) populated from command line, the arguments array is very much like any other string array.

 

The name of Java application is not the part of command line arguments array. Therefore, if you do not pass any argument, then the length of this array will be zero.

 

Whenever you wish to take some action based on the arguments passed to the main method, it is a good practice to check if the command line arguments are valid. You can also display a proper usage if the arguments are not appropriate. For instance, if the Calculator program always expects three arguments and if it is executed with two arguments, you can write a code to display a message saying what arguments are expected by this application in the following manner:

 

public class Calculator {

 public static void main(String[] args) {

   if(args.length != 3)

     System.out.println(“Calculator needs 3 arguments”);

     System.out.println(“Usage: [number1][number2][operation]”);

   } else

     // take action

 }

}

 

That is all you need to know about the main method. Besides the main method, a Java class has yet another useful feature of automatically initializing its member variables. In the next section, we will discuss that and how and when the member variables are initialized and also see why the local variables need explicit initialization.