go to previous page   go to home page   go to next page hear noise

Answer:

Yes.


Running a Method

Here is the example program, now with additional statements:

class StringDemo2
{
  public static void main ( String[] args )
  {
    String str;
    int    len;

    str = new String( "Elementary, my dear Watson!" );

    len = str.length();    // call the length() method of the object

    System.out.println("The length is: " + len );

  }
}

The expression

str.length();

runs the length() method of the object referred to by str. This method counts the number of characters in the data of the object. In our object, it counts the number of characters in "Elementary, my dear Watson!" which is 27. That value is then assigned to the int variable len. Space characters and punctuation characters are included in the length of a string.

Calling a method means asking a method to run. This program called the length() method. (Sometimes the phrase invoking a method is used to mean the same thing.)

Methods frequently return a value. The length method returned the value 27. (Sometimes a method is said to evaluate to a value.) It is as if the method call is replaced by the value it returns. So that

len = str.length();  

is replaced with

len = 27;  

QUESTION 9:

Complete the analogy:

object        you

reference     your cell phone number

variable      piece of paper with your cell phone number written on it

method call   ???

return value  ???