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 a reference variable for 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 future 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 literal like "Hello". (If the String literal already exists somewhere in the system, you get a reference to it. Otherwise an object is constructed and you get a reference to it.)


QUESTION 3:

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

 


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