More about Objects and Classes

The relational operations on primitive data are ==, >=, <=, >, <, and !=. They compare two data values, when those values' type has an ordering. For example, integers are ordered by size or magnitude. The result of a relational operation is a boolean value: either True or False. The relational operators on objects like Strings are different, and they are expressed as methods. Pay special attention to the equality method, equals().

18. Testing Two Reference Variables


Answer:

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

Testing Two Reference Variables

twoSameData

Here is the program, modified to use the == operator.

The objects are the same as before so the picture is the same as on the previous page.


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 == operator looks at the references in the two reference variables to see if those reverences are identical.

Question 18:

What is the output of this program?