Versions Compared

Key

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

...

This code uses == to compare two integer Integer objects. According to EXP03-J. Do not compare String objects using equality or relational operators, for == to return true for two object references, they must point to the same underlying object. Results of using the == operator in this case will be misleading.

...

In this noncompliant code example, it is desired to count the integers of arrays list1 and list2. As class Integer can only cache caches integers from -127 to 128, when an int number value is beyond this range, it is autoboxed into the corresponding wrapper type. The == operator returns false when these distinct wrapper objects are compared. As a result, the output of this example is 0.

...

If it were possible to expand the Integer cache inside Integer (cache (for example, caching all the integer values -32K -32K, which means that all the int values may in the example would be autoboxed to the same cached Integer object objects), then the results may have differed.

...