title: Differences in auto-unboxing between Java 6 vs Java 7
body: <p>I have noted a difference in auto unboxing behavior between Java SE 6 and Java SE 7. I'm wondering why that is, because I can't find any documentation of changes in this behavior between these two versions. </p>
  
  <p>Here's a simple example:</p>
  
  <pre><code>Object[] objs = new Object[2];
  objs[0] = new Integer(5);
  int myInt = (int)objs[0];
  </code></pre>
  
  <p>This compiles fine with javac from Java SE 7. However, if I give the compiler the "-source 1.6" argument I get an error on the last line:</p>
  
  <pre><code>inconvertible types
  found   : java.lang.Object
  required: int
  </code></pre>
  
  <p>I tried downloading the Java SE 6 to compile with the native version 6 compiler (without any -source option). It agrees and gives the same error as above.</p>
  
  <p>So what gives? From some more experimentation it seems that the unboxing in Java 6 can only unbox values that clearly (at compile time) is of the boxed type. For instance, this works in both versions:</p>
  
  <pre><code>Integer[] objs = new Integer[2];
  objs[0] = new Integer(5);
  int myInt = (int)objs[0];
  </code></pre>
  
  <p>So it seems that between Java 6 and 7, the unboxing feature was enhanced so that it could cast and unbox object types in one swoop, without knowing (at compile time) that the value is of the proper boxed type. However, reading through the Java Language Specification or blog postings that were written at the time Java 7 came out, I can't see any change of this thing, so I'm wondering what the change is and what this "feature" is called?</p>
  
  <p>Just a curiosity: Due the change, it is possible to trigger "wrong" unboxings:</p>
  
  <pre><code>Object[] objs = new Float[2];
  objs[0] = new Float(5);
  int myInt = (int)objs[0];
  </code></pre>
  
  <p>This compiles fine but gives a ClassCastException at runtime.</p>
  
  <p>Any reference on this?</p>