This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
public static void main(String args)
Integer i=new Integer(4);
Integer y=new Integer(4);
checkToString cs = new checkToString(2);
checkToString c = new checkToString(2);
System.out.println(" Both objects are equal");
System.out.println(" Unequal objects ");
System.out.println(" i,y - Both objects are equal");
System.out.println(" i,y - Unequal objects ");
In my above class, i & y are giving true on using i.equals(y) and cs & c objects are giving false on using cs.equals(c). In both the cases we are not overriding equals() method. Can someone helps me here?
As I stated in the other thread where you asked this question, what we do in our class doesn't matter here. It's Sring's and Integers equals() methods that are being called (as we can see clearly from the code), so it's what String and Integer do with equals() that matters. And both of those classes override equals(), as you can see in their javadocs.