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



The letter 'D' of "Dog" is compared with the letter 'd' of "doghouse" and so "Dog" comes first.

Array of String References

Although people speak of an "array of objects" or an "array of Strings," in Java this is implemented as an array of references to objects. Here, for example, is an array declaration in Java:

String[] strArray = { "bat", "ant", "dog", "cat", "eel", "ibx", "fox", "gnu", "cow", "pig" };

And here is the array that is created:

array of string references

Notice that the cells of the array are all the same size and that the cells contain references (pointers) to the actual String objects. Each String object is implemented somewhere in memory. It does not really matter where, because the references in the array lead to the object no matter where it is. (You may wish to review chapter 49 if this is not clear.)

Inspect this statement:

String myPet = strArray[2] ;

This copies the reference in cell number 2 or the array into the variable myPet. It does not make a copy of a String. The String object containing the characters "dog" stays in memory where it was previously, but now there are two poiners to it.


What would these statements do?

String temp = strArray[5];
strArray[5] = strArray[3];
strArray[3] = temp;