String[] seasons = new String [4];This creates an array whose four elements are references to objects of type String. Since they are reference types, they will be initialized to null giving the structure illustrated in the next diagram.
The values of the seasons could then be assigned by writing
season [0] = "Spring"; season [1] = "Summer"; season [2] = "Fall" ; season [3] = "Winter";The results of this are displayed in the next diagram.
All of this could have been accomplished in one step by using the array initializer notation that we have seen many times before. To create and initialize the seasons array, we could write
String[] seasons = {"Spring", "Summer", "Fall", "Winter"};You may have noticed that we have already seen string arrays many times. In fact, you have been using them (probably without realizing it) since your very first program! Every main method in Java starts with a header of the form
public static void main (String[] args)The parameter here is a String array called args. Parameters in Java are given the values of their arguments when a method is called. Since the main method is not called from another method, values are given to arguments of the main method somewhat differently than for other methods. To be able to pass argument values to main, you must be able to use a command line interface to run a program. If you are used to running your Java programs in an integrated development environment (IDE), it may be possible to do so from within the IDE but you may have to open a command window. You should check the documentation of your IDE or speak with your system administrator for the details.
Using a command line, we call the main method by writing the word java followed by the name of the class that contains main. If we want to give arguments to main, we can do so simply by writing them on the same line that we use to make the call. Because of this, these arguments are known as command line arguments. They are automatically assigned to the string array parameter of main(usually called args). The first argument is assigned to args[0], the second to args[1] , and so on. Any command line arguments are separated from each other by at least one blank. It is not necessary to surround each argument by double quotes unless we want an argument to contain blanks.
|
Command line arguments can represent any type but they are always stored as strings and, if we want to use them for other purposes, we must convert them appropriately. Often, there are methods available to assist us in these conversions. For example, if we want to convert a string to an integer, we can use parseInt, a class method in the Integer class having the header
public static int parselnt (String s)The method returns the integer represented by s. If s does not represent an integer, the method throws a NumberFormatException.
|
Parsing methods are available for converting strings to any of Java's numerical types. For example, to convert a string to a double value, we could use the method parseDouble in the Double class. A complete list of these methods is given in the table on page 588.
If a program is expecting a particular form of command line argument, then it is easy to check to see whether or not the user has supplied the appropriate number of values.
|
In Example 3, the statement System.exit(1); is used to terminate the program. The exit method of the System class causes a program to stop immediately. An argument of zero is passed to exit on normal termination while some other value (one in our example) is used to indicate abnormal termination.
|