Could the ==
operator be used in this program
instead of the equals()
method?
No. The ==
operator tests
if two reference variables refer to the same object.
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.