Why doesn == work when comparing Strings?
In Java, the equality operator == is (primarily) used in comparisons between primitive data types such as integers. It is bad practice to use it to compare objects like Strings or Points because it just tests whether or not the two objects occupy the same space, i.e. it determines if your two names are just aliases for the same object. If you want to compare the contents of two distinct objects to see if they are equivalent, then you must use the equals methods provided by the class. For example, the following code will print the Yum message, String dinner = “hot dog”; if (dinner.substring(0, 3).equals(“hot”)) System.out.println(“Yum…”); but this code will not print anything: String dinner = “hot dog”; if (dinner.substring(0, 3) == “hot”) System.out.println(“Yum…”); There is an equals method defined for all of the graphical objects: Point origin = new Point(); if (origin.equals(new Point(0, 0))) System.out.println(“This message WILL be printed!”); • I think my diskette is full beca