go to previous page   go to home page   go to next page

Answer:

Looks like a good place for an array.

Array of String References

array of string references

 

You can declare an array of String references:

String[] strArray;          // 1.

This declares a variable strArray which in the future may refer to an array object. Each cell of the array may be a reference to a String object (but so far there is no array).

To create an array of 8 String references do this:

strArray = new String[8] ;  // 2.

Now strArray refers to an array object. The array object has 8 cells. However none of the cells refer to an object (yet). The cells of an array of object references are automatically initialized to null, the special value that means "no object".

Now, store a String reference in cell zero of the array, do this:

strArray[0] = "Hello" ;     // 3.

You don't have to explicitly use the new operator to get a reference to a String object. (If a suitable object already exists, you get a reference to that object. Otherwise an object is constructed and you get a reference to it.) Review String literals in chapter 26 if this is not clear.

QUESTION 3:

Write a statement that puts a reference to "World" in cell 1 of the array.