...
Further confusion arises because the numerical comparison operators <
, <=
, >
, and >=
can be used with the numeric boxed types Byte
, Character
, Short
, Integer
, Long
, Float
, and Double
. In this case, auto-unboxing results in the numeric values contained in the boxed objects being compared, with the expected results . However, auto-unboxing is not applied when the equality operators ==
and !=
are used with these numeric boxed types, so the object references are compared(see JLS Section 5.6.2, "Binary Numeric Promotion"). When both arguments of a ==
or !=
operator are numeric boxed type, the equality operators are defined to be reference equality operators; the operation is thus a reference comparison rather than the anticipated numeric comparison, which may produce unexpected results.
...
Wiki Markup |
---|
\[[FindBugs 2008|AA. Bibliography#FindBugs 08]\] ES: Comparison of String objects using == or \!= \[[JLS 2005|AA. Bibliography#JLS 05]\] [Section 3.10.5|http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5], "String Literals" and [Section 5.6.2|http://java.sun.com/docs/books/jls/third_edition/html/lexicalconversions.html#3html#5.106.5]2], "Binary Numeric Promotion" \[[MITRE 2009|AA. Bibliography#MITRE 09]\] [CWE ID 595|http://cwe.mitre.org/data/definitions/595.html] "Incorrect Syntactic Object Comparison", [CWE ID 597|http://cwe.mitre.org/data/definitions/597.html] "Use of Wrong Operator in String Comparison" |
...