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().

16. The equals() Method


Answer:

The x value and the y value of one point is equal to the x value and the y value of the other point.

The equals() Method

The equals() method is defined for class Point:

pointA.equals( pointB )   —— returns true if the two points
                             contain equivalent data

The example program shows this:

import java.awt.*;
class EqualsDemo
{
  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.equals( pointB ) )
      System.out.println( "The two objects contain the same data: " + pointA );
    else
      System.out.println( "The two objects are not equivalent: " + pointA + 
          " differs from" + pointB);     

  }
}


Question 16:

What is the output of this program? (You might wish to copy-paste-and-run
this program to check your answer.)