Richard G Baldwin (512) 223-4758, baldwin@austin.cc.tx.us, http://www2.austin.cc.tx.us/baldwin/
Command-Line Arguments
Java Programming, Lecture Notes # 32, Revised 10/03/99.
Preface
Students in Prof. Baldwin's Introductory Java Programming classes at ACC are responsible for knowing and understanding all of the material in this lesson (except that they are not responsible for detailed information that is specific to C++).
The detailed material on C++ is provided as supplementary material for the benefit of those persons who are already familiar with C++ and who are making the transition into Java.
Discussion
Familiar example from DOS
In both Java and C++, the program can be written to accept command-line-arguments.
DOS users will be familiar with commands such as the following:
copy fileA fileB |
In this case, copy is the name of the program to be executed, and fileA and fileB are command-line arguments.
C++ syntax for command-line arguments
Java and C++ handle command-line arguments in a different manner. C++ programmers will be familiar with the following syntax which is used in the main() function to capture command-line arguments. |
void main(int argc, char *argv[]){ |
In this case, the operating system passes in two parameters to the function. argc is an integer specifying the number of command-line arguments, while argv is an array of pointers where each pointer in the array points to one of the command-line arguments stored as a string somewhere in memory. In some systems, argv[0] points to a string that is the name of the program. In other systems, argv[0] points to garbage. In C++, if the program is not written to accept command-line arguments, it is not necessary to specify the parameters in the argument list of the function signature. |
Java syntax for command-line arguments
The Java syntax for command-line arguments is as follows. Note that in Java, the parameters to main() must appear in the method signature whether or not the program is written to support the actual use of command-line arguments.
public static void main(String[] args){ |
Where are the arguments stored?
In this case, each of the elements in the array named args (including the element at position zero) is a reference to one of the command-line arguments each of which is a String object.
What about the name of the program?
Unlike C++, the name of the program is not provided in Java. |
How many arguments did the user enter?
Recall from an earlier lesson on arrays that the number of elements in a Java array can be obtained from the length property of the array. Therefore, it is not necessary for the operating system to also pass a parameter specifying the number of arguments.
Sample Java program
The use of command-line arguments is illustrated in the following example Java program. The output from running this program for a specific input is shown in the comments at the beginning of the program.
/*File cmdlin01.java Copyright 1997, R.G.Baldwin |
Sample C++ program
For comparison purposes, the following C++ program is structured to emulate the behavior of the Java program. Again, the output from running the program with a specific command-line input is shown at the beginning of the program. Note that the system used to compile and run this program did display the name of the program in the output. |
/*File cmdlin01.cpp Copyright 1997, R.G.Baldwin |
Review
Q - Provide a common example of a command-line statement that illustrates the use of command-line-arguments.
A - Java programs can be written to accept command-line-arguments. DOS users will be familiar with commands such as the following:
copy fileA fileB |
In this case, copy is the name of the program to be executed, and fileA and fileB are command-line arguments.
Q - Describe the purpose of command-line-arguments.
A - Command-line-arguments are used in many programming and computing environments to provide information to the program at startup that it will need to fulfill its mission during that particular invocation.
Q - In Java, syntax provisions must be made in the method signature for the main method to accommodate command-line-arguments even if the remainder of the program is not designed to make use of them: True or False. If False, explain why.
A - True. In Java, syntax provisions must be made in the method signature for the main method to accommodate command-line-arguments even if the remainder of the program is not designed to make use of them.
Q - Provide the method signature for the main method in a Java application that is designed to accommodate the use of command-line-arguments. Identify the part of the method signature that applies to command-line-arguments and explain how it works.
A - The Java syntax for command-line arguments is as follows.
public static void main(String[] args){ |
In this case, each of the elements in the array named args (including the element at position zero) is a reference to one of the command-line arguments each of which is a String object.
Q - Explain how a Java application can determine the number of command-line-arguments actually entered by the user.
A - The number of elements in the array of references to the String objects that contain the command-line-arguments can be obtained from the length property of the array.
Q - Write a Java program that meets the following specifications.
/*File SampProg11.java from lesson 32 |
-end-
No comments:
Post a Comment