JDK-7147289 : compile errror instead of byte to integer widening conversion
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_7
  • CPU: x86
  • Submitted: 2012-02-21
  • Updated: 2012-09-06
  • Resolved: 2012-02-21
Description
FULL PRODUCT VERSION :
java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) Client VM (build 21.1-b02, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
When compiling the given test case with javac we get a compile time error for Integer i2 = (byte)3. Explicitly using Integer.valueOf( (byte)3); on the other hand works.

The eclipse compiler compiles both variants fine and I don't see anything in the JLS wrt autoboxing that explains the behavior  of javac. Since the compiler should create a valueOf call in the background anyhow and doing that explicitly works fine.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the test case with javac.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No compile time error.
ACTUAL -
Test.java:4: error: incompatible types
                Integer i2 = (byte)3;
                             ^
  required: Integer
  found:    byte

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class Test {
	public static void main(String[] args) {
		Integer i1 = Integer.valueOf((byte)3);  // works fine with all compilers
		Integer i2 = (byte)3;  // works fine with eclipse compiler, not javac
	}
}

---------- END SOURCE ----------

Comments
EVALUATION This behavior is explained in JLS SE 7 - see table 5.1 http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
21-02-2012