/* * let Java teach us about object equality * @author Biagioni, Edoardo * @assignment lecture 2 * @date January 16, 2008 * @bugs none */ public class Equals { /* some objects to compare */ /* a and b are the same string, but not the same object */ static String a = new String ("abc"); static String b = new String ("abc"); static String c = new String ("cba"); /* this main method is invoked when the program is started * * @param arguments ignored * */ public static void main(String [] arguments) { System.out.println ("a = " + a + ", b = " + b + ", c = " + c); test(); System.out.println (""); // now set a to refer to the same object as c a = c; System.out.println ("a = " + a + ", b = " + b + ", c = " + c); test(); } /* this method prints whether a equals b or a equals c * * @param none * */ static void test() { if (a == b) { System.out.println("a == b"); } else { System.out.println("a != b"); } if (a.equals (b)) { System.out.println("a.equals(b)"); } else { System.out.println("! a.equals(b)"); } if (a == c) { System.out.println("a == c"); } else { System.out.println("a != c"); } if (a.equals (c)) { System.out.println("a.equals(c)"); } else { System.out.println("! a.equals(c)"); } } }