Complete Runable Example

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

No. Sometimes strArray[j].length() will ask for the length of a non-existent String.

Complete Runable Example

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

QUESTION 8:

What will the following statement do? Assume that it immediately follows the last line of the program:

strArray[0] = "Good-by" ;