Equivalence of Aliases

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

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

Equivalence of Aliases

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 an object.
  2. Uses the reference pointB to get the x and y from an 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?