Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Java, use == on references to ask whether two variables refer to the same object. Use equals() to ask whether the receiver’s equality rule considers two objects equal. That rule may compare values, but it depends on the class: the default implementation inherited from Object is identity-based.

What is the difference between == and equals()?

For reference values, == compares the references themselves. It is true when both references designate the same object; it does not call an overridden equals() method. The Java Language Specification classifies these as reference equality operators: Java SE 26 Language Specification, equality operators.

equals() is a method call, so its behavior comes from the implementation selected for the object on which it is called. The method inherited from Object returns true for non-null references only when they refer to the same object. A class can override it to define a different equality rule, commonly one based on values: Oracle Java SE 21 Object.equals API.

Example: same value, different objects

Each new String expression creates a distinct object. String defines equality by its contents, so the two comparisons produce different results:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
String a = new String("java");
String b = new String("java");

System.out.println(a == b);      // false: distinct objects
System.out.println(a.equals(b)); // true: equal string contents

A custom class that does not override equals() uses the identity-based implementation inherited from Object. If it does override the method, distinct instances can be equal according to the class’s rule.

When should you use == or equals()?

  • Use == when identity is what matters—for example, to check whether a reference is null or whether two references point to the same instance.
  • Use equals() when you want to know whether two objects count as equal under the receiver class’s equality rule.

Do not assume that equals() always means “same value.” A class may inherit Object.equals unchanged, or define an equality rule that does not simply compare every field. Consult the class’s documented behavior when the distinction matters.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What makes an equals() implementation correct?

The Object API specifies that an equality implementation should be reflexive, symmetric, transitive, and consistent while the information used in the comparison remains unchanged. It should also return false when compared with null. In practical terms, if a.equals(b) is true, then b.equals(a) must also be true; if a equals b and b equals c, then a must equal c.

Equality also has a required relationship with hashing: equal objects must have the same hash code. The API therefore says it is generally necessary to override hashCode() whenever you override equals(). Unequal objects may share a hash code, though distinct hashes can improve hash-table performance. See the Oracle Java SE 21 Object.hashCode API.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick comparison

Operation What it compares What determines the result
== on references Whether both references designate the same object Reference identity; it does not invoke equals().
equals() Whether the receiver considers the argument equal The implementation selected for the receiver; Object uses identity, while an override may define value-based equality.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.