Versions Compared

Key

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

...

Expressions autobox into the intended type when the reference type causing the boxing conversion is one of the specific numeric wrapper types (e.g. for example, Boolean, Byte, Character, Short, Integer, Long, Float, or Double). However, autoboxing can produce unexpected results when the reference type causing the boxing conversion is nonspecific (e.g.for example, Number or Object) and the value being converted is the result of an expression that mixes primitive numeric types. In this latter case, the specific wrapper type that results from the boxing conversion is chosen based on the basis of the numeric promotion rules governing the expression evaluation. Consequently, programs that use primitive arithmetic expressions as actual arguments passed to method parameters that have non-specific nonspecific reference types must cast the expression to the intended primitive numeric type before the boxing conversion takes place.

...

Code Block
bgColor#FFCCCC
public class ShortSet {
  public static void main(String[] args) {
    HashSet<Short> s = new HashSet<Short>();
      for (short i = 0; i < 100; i++) {
        s.add(i);
        s.remove(i - 1);  // tries to remove an Integer
      }
    System.out.println(s.size());
  }
}

...

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 parallels matches the intended boxed type.

Code Block
bgColor#CCCCFF
public class ShortSet {
  public static void main(String[] args) {
    HashSet<Short> s = new HashSet<Short>();
      for (short i = 0; i < 100; i++) {
        s.add(i);
        s.remove((short)(i - 1));  // removes a Short
      }
    System.out.println(s.size());
  }
}

...

Detection of invocations of Collection.remove() whose operand fails to match the type of the elements of the underlying collection is straightforward. It is possible, albeit although unlikely, that some of these invocations could be intended. The remainder are heuristically likely to be in error. Automated detection for other APIs could be possible.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0baedb88bfda7b92-a2770c02-41d54466-a1de8d64-26bba756527b5568258bfe82"><ac:plain-text-body><![CDATA[

[[Core Java 2004

AA. Bibliography#Core Java 04]]

Chapter 5

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="952c273bd20a6d63-e22d5e26-48bc47ae-b9a492ac-90c58c334b0605bf4d5ed233"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[§5.1.7, " Boxing Conversions "

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="324c7df54cf095d0-716a3898-477342b8-a027b64a-8ba7b5824ee76d91229452f4"><ac:plain-text-body><![CDATA[

[[Techtalk 2007

AA. Bibliography#Techtalk 07]]

" The Joy of Sets"

]]></ac:plain-text-body></ac:structured-macro>

...