Previous | Next | Trail Map | How the Java Language Differs From C and C++ | How the Java Language Differs from C and C++


Java Command Line Arguments Differ from C and C++

The command line arguments passed to a Java application are different in number and in type than those passed to a C or C++ program.

Number of Parameters

In C and C++ when you invoke a program, the system passes two parameters to it:
argc--the number of arguments on the command line
argv--a pointer to an array of strings that contain the arguments

When you invoke a Java application, the system only passes one parameter to it:

args--an array of Strings (just an array--not a pointer to an array) that contain the arguments

You can derive the number of command line arguments with the array's length() method.

The First Command Line Argument

In C and C++, the system passes the entire command line to the program as arguments, including the name used to invoke it. For example, if you invoked a C program like this:
diff file1 file2
Then the first argument in the argv parameter is diff.

In the Java language, you always know the name of the application because it's the name of the class where the main method is defined. So, the Java runtime system does not pass the class name you invoke to the main method. Rather, the system passes only the items on the command line that appear after the class name. For example, if you invoked a Java application like this:

java diff file1 file2
The first command line argument is file1.


Previous | Next | Trail Map | How the Java Language Differs From C and C++ | How the Java Language Differs from C and C++