/* * File: TestEquals.java * Author: Java, Java, Java * Description: This program illustrates the differences * between the == operator and the equals() method as * applied to objects in general. The == operator implements * object identity, where two object references are identical if * they refer to the same object. The default implementation of * Object.equals() just implements object identity. The default * equals() method should be overridden to define equality in * a way that is appropriate to a given subclass of Object. */ import javax.swing.*; public class TestEquals { static JButton b1 = new JButton ("a"); static JButton b2 = new JButton ("b"); static JButton b3 = b2; private static void isEqual(Object o1, Object o2) { if (o1.equals(o2)) System.out.println(o1.toString() + " equals " + o2.toString()); else System.out.println(o1.toString() + " does NOT equal " + o2.toString()); } // isEqual() private static void isIdentical( Object o1, Object o2 ) { if (o1 == o2) System.out.println(o1.toString() + " is identical to " + o2.toString()); else System.out.println(o1.toString() + " is NOT identical to " + o2.toString()); } // isIdentical() public static void main(String argv[]) { isEqual(b1, b2); // not equal isEqual(b1, b3); // not equal isEqual(b2, b3); // equal isIdentical(b1, b2); // not identical isIdentical(b1, b3); // not identical isIdentical(b2, b3); // identical } // main() } // TestEquals