strArray[1] = "World" ;
Recall these facts about all arrays:
String
".
Frequent Bug: It is easy to confuse the length of an array with the number
of cells that contain a reference.
In the example, only two cells contain references to String
s, but
strArray.length
is 8.
Abundant Source of Hopeless Confusion: Often people say "an array of Strings" or "a String in the array" when really they should say "an array of String references" or "a String referenced by a cell of the array". This can lead to confusion. Be careful when you encounter such phrases.
Here is code that declares and constructs the array in one statement,
and then puts
some String
references into it:
// combined statement String[] strArray = new String[8] ; strArray[0] = "Hello" ; strArray[1] = "World" ; strArray[2] = "Greetings" ; strArray[3] = "Jupiter" ;
Must all the String
s in the array be of the same length?
Remember, this really is asking
"must all of the String
s pointed to by cells of the array be of the same length?".