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

Answer:

Yes. You have to be careful how you use the result, however.


Equivalence of Aliases

two variables, both pointing at the same object

Here is the example program (again!) with this modification:

import java.awt.*;
class EqualsDemo4
{
  public static void main ( String arg[] )
  {
    Point pointA = new Point( 7, 99 );     // pointA refers to a Point Object
    Point pointB = pointA;                 // pointB refers to the same Object 

    if ( pointA.equals( pointB ) )
    {
      System.out.println( "The two variables refer to the same object," );
      System.out.println( "or different objects with equivalent data." );
    }
    else
      System.out.println( "The two variables refer to different objects" );     

  }
}

The picture of the situation is the same as before. But now the equals() method is used. It:

  1. Uses the reference pointA to get the x and y from the object.
  2. Uses the reference pointB to get the x and y from the same object.
  3. Determines that the two x's are the same and that the two y's are the same.
  4. Returns a true.

The fact that the object is the same object in step 1 and step 2 does not matter. The x's that are tested are the same, and the y's that are tested are the same, so the result is true.


QUESTION 21:

If the == operator returns a true will the equals() method return a true, always?


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