Testing Two Reference Variables

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

Could the == operator be used in this program instead of the equals() method?

Answer:

No. The == operator tests if two reference variables refer to the same object.

Testing Two Reference Variables

Here is the program, with changes:

import java.awt.*;
class EqualsDemo2
{
  public static void main ( String arg[] )
  {
    Point pointA = new Point( 7, 99 );   // first Point
    Point pointB = new Point( 7, 99 );   // second Point with equivalent data

    if ( pointA == pointB  )
      System.out.println( "The two variables refer to the same object" );   
    else
      System.out.println( "The two variables refer to different objects" );  

  }
}

The picture of the situation (after the two new operators have executed) is the same as on the previous page.

QUESTION 18:

What is the output of this program?