No. Sometimes strArray[j].length()
will ask for the
length of a non-existent String
.
Here is the complete program:
class StringArray { public static void main ( String[] args ) { String[] strArray = new String[8] ; strArray[0] = "Hello" ; strArray[1] = "World" ; strArray[2] = "Greetings" ; strArray[3] = "Jupiter" ; strArray[ strArray.length-1 ] = "the end" ; for (int j=0; j < strArray.length; j++ ) if ( strArray[j] != null ) System.out.println( "cell " + j + ": " + strArray[j] ); else System.out.println( "cell " + j + ": " + "empty" ); } }
You may wish to copy this program to your editor and run it. First-string programmers that run this code will see:
cell 0: Hello cell 1: World cell 2: Greetings cell 3: Jupiter cell 4: empty cell 5: empty cell 6: empty cell 7: the end
What will the following statement do? Assume that it immediately follows the last line of the program:
strArray[0] = "Good-by" ;