Command-Line Arguments

Paul Ngugi - May 28 - - Dev Community

The main method can receive string arguments from the command line. Perhaps you have already noticed the unusual header for the main method, which has the 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? Yes, of course you can. In the following examples, the main method in class TestMain is invoked by a method in A.

Image description

A main method is just a regular method. Furthermore, you can pass arguments from the command line.

Passing Strings to the main Method

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, alpha, and 53. Since First num is a string, it is enclosed in double quotes. 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 Java interpreter 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.

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 0. args references to this empty array. Therefore, args is not null, but args.length is 0.

Case Study: Calculator

Suppose you are to develop a program that performs arithmetic operations on integers. The program receives an expression in one string argument. The expression consists of 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 below shows sample runs of the program.

Image description

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.

Here are the steps in the program:

  1. Use args.length to determine whether the expression has been provided as three arguments in the command line. If not, terminate the program using System.exit(1).
  2. Perform a binary arithmetic operation on the operands args[0] and args[2] using the operator in args[1].

The program is shown below:

Image description

Integer.parseInt(args[0]) (line 17) converts a digital string into an integer. The string must consist of digits. If not, the program will terminate abnormally.

We used the . symbol for multiplication, not the common ***** symbol. The reason for this is that the ***** symbol refers to all the files in the current directory when it is used on a 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 = 0; i < args.length; i++)
System.out.println(args[i]);
}
}

To circumvent this problem, we will have to use a different symbol for the multiplication operator.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player