...
If the value
pbeing boxed istrue,false, abyte, acharin the range\u0000to\u007f, or anintorshortnumber between-128and127, then letr1andr2be the results of any two boxing conversions ofp. It is always the case thatr1 == r2.
Primitive Type | Boxed Type | Fully Memoized |
|---|---|---|
|
| Yes |
|
| No |
Use of the == and != operators for comparing the values of fully memoized boxed primitive types is permitted.
...
Using the equivalence operators to compare values of boxed primitives can lead to erroneous comparisons.
Rule | Severity | Likelihood |
|---|
Detectable | Repairable | Priority | Level |
|---|---|---|---|
EXP03-J | Low | Likely | Yes |
Yes |
P9 | L2 |
Automated Detection
Detection of all uses of the reference equality operators on boxed primitive objects is straightforward. Determining the correctness of such uses is infeasible in the general case.
Tool | Version | Checker | Description |
|---|
| CodeSonar |
|
|
|
FB.EQ_ALWAYS_FALSE
FB.EQ_ALWAYS_TRUE
FB.EQ_CHECK_FOR_OPERAND_NOT_ COMPATIBLE_WITH_THIS
| JAVA.COMPARE.EMPTYSTR | Comparison to empty string | |||
| Coverity | 7.5 | BAD_EQ |
ABSTRACT_SELF |
ALWAYS_ |
FALSE |
_ STRING_WITH_EQ | Implemented | ||||||||
| Klocwork |
| CMP.OBJ | |||||||
| Parasoft Jtest |
| CERT.EXP03.UEIC | Do not use '==' or '!=' to compare objects | ||||||
| PVS-Studio |
| V6013 | |||||||
| SonarQube |
| S1698 | "==" and "!=" should not be used when "equals" is overridden |
Related Guidelines
CWE-595, Comparison of Object References Instead of Object Contents |
Bibliography
Puzzle 4, "Searching for the One" | |
[JLS 2015] | |
Using == to Compare Objects Rather than | |
| [Seacord 2015] |
...
...