Q:

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

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 memory address is different? please correct me if i am wrong. Thanks

A:

Object.hashCode:

The hashCode method defined by class Object does return distinct
  integers for distinct objects.

But Integer.hashCode overrides Object.hashCode:

returns a hash code value for this object, equal to the primitive int
  value represented by this Integer object.

which means Integers with same hashCode does not have to share the same object.

The source code of Integer.hashCode
@Override
public int hashCode() {
    return Integer.hashCode(value);
}

public static int hashCode(int value) {
    return value;
}

If you let:
Integer a = 127;
Integer b = 127; 
System.out.println(a.equals(b)); // true
System.out.println(a == b); // true

this is because Integers between [-128, 127] could be cached.

