Do you think that the following is correct?

str1.length() = 12 ;  // change the length of str1

A good answer might be:

No. The left-hand side of an assignment statement must be a variable.

If you got this question wrong, review the two steps of an assignment statement (on the previous page.)


Practice Program

It has been a long time since you have had some blue blanks to fill in. I'm sure that you have missed them. Here is a program that will:

  1. Create two String objects.
  2. The first String will get the characters "Green eggs"
  3. The second String will get the characters " and ham."
  4. The program will compute the length of each string, and then
  5. Write out the combined length of both Strings (the sum of the individual lengths.)

class StringTester
{

  public static void main ( String[] args )
  {
    String str1;   // str1 is a reference to a String object.
    ______ ____;   // str2 is a reference to a second String object.

    int    ______ , ______ ;    // the length of str1 and the length of str2

    ____   = _________________________ ; // create the first String

    ____   = _________________________ ; // create the second String


    _____  = str1.length();    // get the length of the first string

    _____  = ____.________;    // get the length of the second string

    System.out.println("The combined length of both strings is " +
        _________________ + " characters" );
  }
}

QUESTION 16:

Mentally fill in the blanks of the program. You will have to choose names for some variables.