-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathRefEqBoxed.qhelp
More file actions
45 lines (31 loc) · 1.31 KB
/
RefEqBoxed.qhelp
File metadata and controls
45 lines (31 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Comparing two boxed primitive values using <code>==</code> or
<code>!=</code> compares object identity, which may not be intended.</p>
</overview>
<recommendation>
<p>Usually, you should compare non-primitive objects, for example boxed primitive values, by using
their <code>equals</code> methods.</p>
</recommendation>
<example>
<p>With the following definition, the method call <code>refEq(new Integer(2), new Integer(2))</code> returns <code>false</code>
because the objects are not identical.</p>
<sample src="RefEqBoxedBad.java" />
<p>With the following definition, the method call <code>realEq(new Integer(2), new Integer(2))</code>
returns <code>true</code> because the objects contain equal values.</p>
<sample src="RefEqBoxed.java" />
</example>
<references>
<li>
J. Bloch and N. Gafter, <em>Java Puzzlers: Traps, Pitfalls, and Corner Cases</em>, Puzzle 32. Addison-Wesley, 2005.
</li>
<li>
Java API Specification:
<a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#equals(java.lang.Object)">Object.equals()</a>,
<a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Integer.html#equals(java.lang.Object)">Integer.equals()</a>.
</li>
</references>
</qhelp>