Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

Primitive Type

Boxed Type

Fully Memoized

boolean, byte

Boolean, Byte

Yes

char, short, int

Char, Short, Int

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
Include Page
CodeSonar_V
CodeSonar_V

JAVA.COMPARE.EMPTYSTR
JAVA.COMPARE.EQ
JAVA.COMPARE.EQARRAY

Comparison to Empty String (Java)
Should Use equals() Instead of == (Java)
equals on Array (Java)

Coverity7.5

BAD_EQ
FB.EQ_ABSTRACT_SELF
FB.EQ_ALWAYS_FALSE
FB.EQ_ALWAYS_TRUE
FB.EQ_CHECK_FOR_OPERAND_NOT_ COMPATIBLE_WITH_THIS
FB.EQ_COMPARETO_USE_OBJECT_ EQUALS
FB.EQ_COMPARING_CLASS_NAMES
FB.EQ_DOESNT_OVERRIDE_EQUALS
FB.EQ_DONT_DEFINE_EQUALS_ FOR_ENUM
FB.EQ_GETCLASS_AND_CLASS_ CONSTANT
FB.EQ_OTHER_NO_OBJECT
FB.EQ_OTHER_USE_OBJECT
FB.EQ_OVERRIDING_EQUALS_ NOT_SYMMETRIC
FB.EQ_SELF_NO_OBJECT
FB.EQ_SELF_USE_OBJECT
FB.EQ_UNUSUAL
FB.ES_COMPARING_PARAMETER_ STRING_WITH_EQ
FB.ES_COMPARING_STRINGS_ WITH_EQ
FB.ES_COMPARING_PARAMETER

_ STRING_WITH_EQImplementedParasoft Jtest9.5PB.CUB.UEIC 

_ STRING_WITH_EQ

Implemented
Parasoft Jtest
Include Page
Parasoft_V
Parasoft_V
CERT.EXP03.UEICDo not use '==' or '!=' to compare objects
PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V6013
SonarQube
Include Page
SonarQube_V
SonarQube_V
S1698"==" and "!=" should not be used when "equals" is overridden

Related Guidelines

MITRE CWE

CWE-595, Comparison of Object References Instead of Object Contents
CWE-597, Use of Wrong Operator in String Comparison

Bibliography

[Bloch 2009]

Puzzle 4, "Searching for the One"

[JLS 2015]

§5.1.7, "Boxing Conversion"

[Pugh 2009]

Using == to Compare Objects Rather than .equals

[Seacord 2015]

...


...

Image Modified Image Modified Image Modified