...
Primitive Type | Boxed Type | Fully Memoized |
---|---|---|
|
| yes |
|
| no |
The Use of the ==
and !=
operators may be used to compare for comparing the values of fully memoized boxed primitive types is permitted.
The Use of the ==
and !=
operators may only be used for comparing the values of boxed primitives primitive types that are not fully memoized is permitted only when the range of values represented is guaranteed to be within the ranges specified by the Java Language Specification to be fully memoized.
In general, avoid using these operators to compare the values of boxed primitives that are not fully memoized.
Use of the ==
and !=
operators for comparing the values of boxed primitive types is forbidden in all other cases.
This rule is a specialization of EXP01-J. Do not confuse abstract object equality with reference equality.
Note that JVM implementations Implementations are allowed, but not required, to memoize additional values:
...
Code that depends on implementation-defined behavior is not non-portable.
Noncompliant Code Example
...
If the particular JVM running this code memoized integer values from -32,768 to 32,767, all of the int
values in the example would have been autoboxed to singleton Integer
objects, and the example code would have operated as expected. Using reference equality instead of object equality requires that all values encountered fall within the interval of values memoized by the JVM. The Java Language Specification does not specify lacks a specification of this interval; rather, it only provides specifies a minimum range that must be memoized. Consequently, successful prediction of this program's behavior would require implementation-specific details of the JVM.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7db47054e630d665-680b16d2-4ac44597-b22caf01-680f04c2e6febbd04cae68b5"><ac:plain-text-body><![CDATA[ | [[Bloch 2009 | AA. Bibliography#Bloch 09]] | 4. "Searching for the One" | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7568618a94d40389-e64c6001-4a2d4583-91e2b97e-77f890da996faa746623ac0f"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [§5.1.7, "Boxing Conversion" | http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d1882e01b70bf35f-d9fbae81-4cc6414f-a276b1b9-d323b7af96945d2064dda04f"><ac:plain-text-body><![CDATA[ | [[Pugh 2009 | AA. Bibliography#Pugh 09]] | Using == to compare objects rather than .equals | ]]></ac:plain-text-body></ac:structured-macro> |
...