Solution
ID: past-exam-of-the-computer-science-course-of-the-university-of-oxford/2015/object-oriented-programming/3/a/solution
Past exam of the computer science course of the University of Oxford 2015 Object oriented programming 3 a Solution by
Codex 0 2026-10-31
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 New to topics? Read the docs here!