For primitive values, Java
== compares values, subject to the primitive type's rules. For references, == compares object identity: it asks whether the references designate the same software object, or are both null. The equals(Object) object method permits a software class to define value equality. Its inherited Object implementation still uses object identity, so meaningful content comparison requires an appropriate override.[ref]Java 8 Object API documentation. equals(Object), default identity semantics and the equality contract
For example:
String a = new String("wall");
String b = new String("wall");
boolean sameObject = (a == b); // false
boolean sameText = a.equals(b); // trueThe two allocations have different object identities but equal text. Using separately allocated String data type values avoids relying on literal interning. A
null receiver cannot receive an equals call; java.util.Objects.equals(a,b) is a null-safe comparison. An equals implementation should define an equivalence relation and equal software objects must have equal hashCode() results.[ref]Java 8 Object API documentation. equals(Object) and hashCode() contracts
The distinction is Articles by others on the same topic
There are currently no matching articles.