...
If the value
p
being boxed istrue
,false
, abyte
, achar
in the range\u0000
to\u007f
, or anint
orshort
number between-128
and127
, then letr1
andr2
be 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 | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP03-J | Low | Likely | Medium | P6 | 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 |
| JAVA.COMPARE.EMPTYSTR | Comparison to Empty String (Java) | ||||||
Coverity | 7.5 | BAD_EQ |
_ STRING_WITH_EQ | Implemented | ||||||||
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] |
...
...