Completion requirements
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().
21. Equivalence of Aliases
Answer:
Yes. You have to be careful how you use the result, however.
alias-detector
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:
- Uses the reference pointA to get the x and y from the object.
- Uses the reference pointB to get the x and y from the same object.
- Determines that the two x's are the same and that the two y's are the same.
- 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?