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 .
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:
phoneBook[ j ] contains a reference to a PhoneEntry object.PhoneEntry object contains an instance variable, name.name is a reference to a String object.getName() method returns that reference.String object has an equals() method.equals() method is used with the String referred to by targetName .true or false, depending on whether the two strings are equivalent.Further Trickyness: 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.
Does the expression
targetName.equals( phoneBook[ j ].getName() )
evaluate to the same value as
phoneBook[ j ].getName().equals( targetName )