Java != Operator: Reference vs Value Comparison
Learn how the Java `!=` operator works for primitives and object references, why it differs from `equals()`, and when to use each kind of comparison.

The != operator in Java is a source of subtle bugs because its meaning depends entirely on whether the operands are primitives or objects. This article explains how Java's != operator behaves in both cases, where it diverges from equals(), and how to choose the right comparison for your code.
The != Operator for Primitive Types
For primitive types like int, double, boolean, char, the != operator compares the actual values. That means 5 != 3 evaluates to true, and 5 != 5 evaluates to false. The operator works directly on the numeric or boolean representation, so there is no ambiguity.
int a = 10;
int b = 20;
boolean notEqual = (a != b); // trueThis is the straightforward use case. For float and double, != also works, but be careful with NaN. In Java, NaN != NaN is true because NaN is not equal to itself according to the IEEE 754 standard. This is often surprising but is the correct behavior for floating-point comparisons.
Reference Comparison for Objects
When you apply != to object references, it compares the references themselves, not the objects' contents. Two distinct objects with identical field values are still != because they occupy different memory locations.
String s1 = new String("hello");
String s2 = new String("hello");
boolean refsNotEqual = (s1 != s2); // true, because s1 and s2 are different objectsThis is the root of many bugs. If you want to compare the logical content of objects, you must use the equals() method, which most classes override to define value equality. The != operator cannot be overridden; it always performs reference comparison for objects.
Why equals() Is Usually What You Want
For domain objects, strings, collections, and most other classes, the intent is almost always value equality. The equals() method is designed for that purpose. For example, String overrides equals() to compare character sequences.
String s1 = new String("hello");
String s2 = new String("hello");
boolean valuesEqual = s1.equals(s2); // trueWhen you write your own classes, you should override equals() (and hashCode()) to define what "equal" means for instances. Then use equals() in business logic, not !=. The only common exception is when you intentionally want to check whether two variables point to the exact same object, such as in identity-based caching or certain concurrency checks.
Common Mistakes with != and Objects
The most frequent mistake is using != to compare strings or custom objects for inequality. For example:
String input = getInput();
if (input != "expected") { // WRONG: compares references, not content
// ...
}This often works by accident because of string interning, but it is unreliable. The correct version is:
if (!"expected".equals(input)) {
// ...
}Notice the constant is placed on the left to avoid a NullPointerException when input is null. Similarly, for custom objects, don't use obj != other when you mean value inequality. If obj is known to be non-null, use !obj.equals(other). If either side may be null, prefer a null-safe helper such as !Objects.equals(obj, other) (Java 7 and later).
Null Checks with != and ==
The != operator is the standard way to check for a non-null reference. Because null is a reference literal, obj != null is a reference comparison that returns true when obj points to any object, and false when it is null. This is safe and idiomatic.
if (obj != null) {
obj.doSomething();
}For the opposite check, obj == null is used. These checks are cheap and do not require equals(). However, do not use equals() to check for null, because calling obj.equals(null) will throw an exception if obj is null. The != null pattern is the recommended way to guard against null references.
Performance and Maintainability Considerations
Reference comparison with != is extremely fast because it only compares memory addresses. For null checks, it is the most efficient approach. In contrast, equals() may involve field-by-field comparison, which can be expensive for large objects. But you should not sacrifice correctness for speed. Use != only where reference identity is the actual requirement, and use equals() for value semantics.
Maintainability also matters. Code that uses != on objects often hides the developer's intent. A reader cannot tell whether reference identity was intentional or accidental. Using equals() makes the value comparison explicit. When you do need identity, consider naming variables or adding a comment to clarify why reference equality is required.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.