Alias

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:

The two variables refer to different objects

Alias

It is possible to have two (or more) reference variables refer to the same object. Here is a modification to the program that does that:

import java.awt.*;
class EqualsDemo3
{
  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 == pointB  )
      System.out.println( "The two variables refer to the same object" );     
    else
      System.out.println( "The two variables refer to different objects" );     

  }
}

Only one object has been created (because there is only one new operator.) The second statement:

Point pointB = pointA; 

copies the reference that is in pointA into the reference variable pointB. Now both reference variables lead to the same object.

alias: When two or more reference variables refer to the same object, each variable is said to be an alias.

QUESTION 19:

What is the output of the program?