Posts

Showing posts with the label hashcode

How == and equal works in java in Case of Integer Object? [duplicate]

How == and equal works in java in Case of Integer Object? [duplicate] This question already has an answer here: I have found many possible duplicates question on this but none clarifies my doubt on how it works? Integer a =25654; // a.hashCode()=>25654 Integer b =25654; // b.hashCode()=>25654 System.out.println(a.equals(b)); => true System.out.println(a == b); => false I have read this answer somewhere.. If no parent classes have provided an override, then it defaults to the method from the ultimate parent class, Object, and so you're left with the Object#equals(Object o) method. Per the Object API this is the same as ==; that is, it returns true if and only if both variables refer to the same object, if their references are one and the same. Thus you will be testing for object equality and not functional equality. in this case both object have same memory address(as per hashcode) still why does it returns false when we compare using == ? or the actual...