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

After the following statements have executed,

String str = new String("alphabet soup");
int    len = str.length();

what value is in the variable len?

Answer:

13. (Remember to count the space character.)

Methods without Parameters

Look again at the second statement, which uses dot notation to specify which method to run:

String str = new String("alphabet soup");

int    len = str.length();
              |     |   |
              |     |   +---- a parameter list with no parameters
              |     |
              |     +---- the name of the method to run
              |
              +---- the reference to the object that contains the method

The second statement contains a method call. This is a request to run a method of an object. The object referenced by str contains a method length() which, when called, returns the number of characters in the String.

The length() method does not require any parameters. The parameter list in the method call str.length() is empty.

QUESTION 2:

What is wrong with the following code segment?

Point myPoint = new Point();  // construct a point at x=0, y=0
myPoint.move();               // move the point to a new location