Java Reference
In-Depth Information
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 8.14 How is an array of objects created?
SR 8.15 Suppose team is an array of strings meant to hold the names of the six
players on a volleyball team: Amanda, Clare, Emily, Julie, Katie, and
Maria.
a. Write an array declaration for team .
b. Show how to both declare and populate team using an initializer
list.
SR 8.16 Assume Book is a class whose objects represent topics. Assume a con-
structor of the Book class accepts two parameters—the name of the
book and the number of pages.
a. Write a declaration for a variable named library that is an array
of ten books.
b. Write a new statement that sets the first book in the library array
to "Starship Troopers", which is 208 pages long.
8.4 Command-Line Arguments
The formal parameter to the main method of a Java application is always an array
of String objects. We've ignored that parameter in previous examples, but now
we can discuss how it might occasionally be useful.
The Java run-time environment invokes the main method when
an application is submitted to the interpreter. The String[] param-
eter, which we typically call args , represents command-line argu-
ments that are provided when the interpreter is invoked. Any extra
information on the command line when the interpreter is invoked is
stored in the args array for use by the program. This technique is another way to
provide input to a program.
The program shown in Listing 8.10 uses command-line arguments to print a
nametag. It assumes the first argument represents some type of greeting and the
second argument represents a person's name.
If two strings are not provided on the command line for the NameTag program,
the args array will not contain enough (if any) elements, and the references in the
program will cause an ArrayIndexOutOfBoundsException to be thrown. If extra
information is included on the command line, it will be stored in the args array
but ignored by the program.
KEY CONCEPT
Command-line arguments are stored
in an array of String objects and
are passed to the main method.
 
Search WWH ::




Custom Search