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

Answer:

If the first half of the expression is true, the entire expression

phoneBook[ j ] != null && phoneBook[ j ].getName().equals( targetName )

could be true or false .


Completed Search

  public PhoneEntry search( String targetName )  
  {
    for ( int j=0; j < phoneBook.length; j++ )
    {
      if ( phoneBook[ j ] != null && phoneBook[ j ].getName().equals( targetName ) )
        return phoneBook[ j ];
    }

    return null;
  }

You may be uncomfortable with this expression:

phoneBook[ j ].getName().equals( targetName )

Look at it piece by piece:

Further Trickiness: Note that the method returns a reference to an object:

public PhoneEntry search( String targetName )  

It is fine for a method to return (to evaluate to) a pointer to an object. That pointer can then be used to invoke the methods of the object.

This is complicated. If you are still uncomfortable don't worry too much. Read through the code and the explanations a few times and then move on.


QUESTION 14:

Does the expression

targetName.equals( phoneBook[ j ].getName() )

evaluate to the same value as

phoneBook[ j ].getName().equals( targetName )

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