Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

Java == vs. equals(): Compare References, Values, and Strings Correctly

Java == compares primitive values or object identity; equals() follows a class’s equality rules. See why string literals can mislead and how to compare nullable values.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Java, == compares primitive values but checks whether two object references point to the same object. For strings and other objects, equals() checks equality as the class defines it. For example, two separately created strings can have identical text while a == b is false and a.equals(b) is true.

What does == compare in Java?

The answer depends on the operands. With primitive values such as int, == compares the values. With object references, it tests identity: whether both references denote the same object. It does not inspect the object’s fields or compare its text.

As an Amazon Associate I earn from qualifying purchases.

The Java Language Specification states that, for reference operands, == is true when both values are null or both refer to the same object or array; otherwise it is false. The rule is documented in the Java SE 21 Language Specification and the Java SE 26 early-access specification. The latter is an early-access, version-sensitive page.

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

Why can two equal strings fail an == comparison?

Each call to new String(...) creates a distinct string object. The text can match without the references identifying the same object:

String a = new String("coffee");
String b = new String("coffee");

System.out.println(a == b);      // false: distinct String objects
System.out.println(a.equals(b)); // true: same character sequence

This code illustrates the documented behavior; it is not a report of a test run. The Java SE 21 String API specifies that String.equals(Object) returns true when the argument is a non-null String representing the same sequence of characters. The comparison is case-sensitive, so "Coffee".equals("coffee") is false.

Why do string literals sometimes make == true?

Java interns string literals and string-valued constant expressions. If two variables are initialized with the same literal, they may refer to the same pooled object, so == can be true. That result reflects shared identity, not a comparison of character contents.

A separately constructed string can contain the same characters but have a different identity, which is why the previous example can produce false for ==. The String API also documents intern(), which returns a canonical pooled representation: for strings s and t, s.intern() == t.intern() is true exactly when s.equals(t) is true. This explains interning; it is not a reason to use intern() as the ordinary way to compare strings.

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

Which comparison should you use?

What you intend to compare Use Meaning
Primitive values, such as two ints == Compares the values.
Whether two references identify the same object == Compares object identity.
String contents String.equals() Checks for the same character sequence, case-sensitively.
Custom-object equality The class’s equals() contract Uses the equality semantics defined by that class.
Two references that may be null Explicit null handling or Objects.equals(a, b) A practical null-safe equality comparison.

How should you compare strings when null is possible?

Calling equals() on a null reference throws a NullPointerException. By contrast, String.equals(null) returns false. If the receiver is guaranteed non-null, use a.equals(b). If either reference may be null, compare explicitly or use Objects.equals(a, b), which handles null values without calling a method on a null receiver.

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

What does equals() mean for other objects?

equals() is a method, so its meaning depends on the class. The default implementation inherited from Object uses identity equality. A class can override it to define value equality, such as equality based on selected fields. It does not have to compare every field; its behavior follows the class’s chosen semantics.

The Java SE 21 Object API documents the equals() contract. A valid implementation must be reflexive, symmetric, transitive, consistent while the information used in comparison remains unchanged, and return false when compared with null. There is also a related hashCode() rule: if two objects are equal according to equals(), they must have the same hash code.

When a custom object’s equality result surprises you, inspect that class’s equals() implementation or documentation. The method name alone does not tell you which fields or properties it considers.

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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.