The Equality Operators
suggest changeThe ==
and !=
operators are binary operators that evaluate to true
or false
depending on whether the operands are equal. The ==
operator gives true
if the operands are equal and false
otherwise. The !=
operator gives false
if the operands are equal and true
otherwise.
These operators can be used operands with primitive and reference types, but the behavior is significantly different. According to the JLS, there are actually three distinct sets of these operators:
- The Boolean
==
and!=
operators. - The Numeric
==
and!=
operators. - The Reference
==
and!=
operators.
However, in all cases, the result type of the ==
and !=
operators is boolean
.
The Numeric ==
and !=
operators
When one (or both) of the operands of an ==
or !=
operator is a primitive numeric type (byte
, short
, char
, int,
long
, float
or double
), the operator is a numeric comparison. The second operand must be either a primitive numeric type, or a boxed numeric type.
The behavior other numeric operators is as follows:
- If one of the operands is a boxed type, it is unboxed.
- If either of the operands now a
byte
,short
orchar
, it is promoted to anint
. - If the types of the operands are not the same, then the operand with the “smaller” type is promoted to the “larger” type.
- The comparison is then carried out as follows:
- If the promoted operands are `int` or `long` then the values are tested to see if they are identical.
- If the promoted operands are `float` or `double` then:
- the two versions of zero (`+0.0` and `-0.0`) are treated as equal
- a `NaN` value is treated as not equals to anything, and
- other values are equal if their IEEE 754 representations are identical.
Note: you need to be careful when using ==
and !=
to compare floating point values.
The Boolean ==
and !=
operators
If both operands are boolean
, or one is boolean
and the other is Boolean
, these operators the Boolean ==
and !=
operators. The behavior is as follows:
- If one of the operands is a
Boolean
, it is unboxed. - The unboxed operands are tested and the boolean result is calculated according to the following truth table
A | B | A == B | A != B | —— | —— | —— | —— | false | false | true | false | false | true | false | true | true | false | false | true | true | true | true | false |
There are two “pitfalls” that make it advisable to use ==
and !=
sparingly with truth values:
- If you use
==
or!=
to compare twoBoolean
objects, then the Reference operators are used. This may give an unexpected result; see http://stackoverflow.com/documentation/java/4388/java-pitfalls/8996/pitfall-using-to-compare-primitive-wrappers-objects-such-as-integer#t=201612061208590356301 - The
==
operator can easily be mistyped as=
. For most operand types, this mistake leads to a compilation error. However, forboolean
andBoolean
operands the mistake leads to incorrect runtime behavior; see http://stackoverflow.com/documentation/java/5382/java-pitfalls-language-syntax/20112/pitfall-using-to-test-a-boolean#t=201612061213124111596
The Reference ==
and !=
operators
If both operands are object references, the ==
and !=
operators test if the two operands refer to the same object. This often not what you want. To test if two objects are equal by value, the .equals()
method should be used instead.
String s1 = "We are equal";
String s2 = new String("We are equal");
s1.equals(s2); // true
// WARNING - don't use == or != with String values
s1 == s2; // false
Warning: using ==
and !=
to compare String
values is incorrect in most cases; see http://stackoverflow.com/documentation/java/4388/java-pitfalls/16290/using-to-compare-strings . A similar problem applies to primitive wrapper types; see http://stackoverflow.com/documentation/java/4388/java-pitfalls/8996/using-to-compare-primitive-wrappers-objects-such-as-integer .
About the NaN edge-cases
JLS 15.21.1 states the following:
If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x != x is true if and only if the value of x is NaN.
This behavior is (to most programmers) unexpected. If you test if a NaN
value is equal to itself, the answer is “No it isn’t!”. In other words, ==
is not reflexive for NaN
values.
However, this is not a Java “oddity”, this behavior is specified in the IEEE 754 floating-point standards, and you will find that it is implemented by most modern programming languages. (For more information, see http://stackoverflow.com/a/1573715/139985 … noting that this is written by someone who was “in the room when the decisions were made”!)