Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: the examples also violate "EXP00-J. Do not ignore values returned by methods"which would have detected the problem.

...

The language's type checking guarantees that only values of type Short can be inserted into the HashSet. Nevertheless, programmers are free to attempt to remove an object of any type because Collections<E>.remove() accepts an argument of type Object rather than of type E. Such behavior can result in unintended object retention or memory leaks [Techtalk 2007].

This noncompliant code example also violates EXP00-J. Do not ignore values returned by methods.

Compliant Solution

Objects removed from a collection must share the type of the elements of the collection. Numeric promotion and autoboxing can produce unexpected object types. This compliant solution uses an explicit cast to short that matches the intended boxed type.

...