United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-7147289 : compile errror instead of byte to integer widening conversion

Details
Type:
Bug
Submit Date:
2012-02-21
Status:
Closed
Updated Date:
2012-09-06
Project Name:
JDK
Resolved Date:
2012-02-21
Component:
tools
OS:
windows_7
Sub-Component:
javac
CPU:
x86
Priority:
P4
Resolution:
Not an Issue
Affected Versions:
7
Fixed Versions:

Related Reports

Sub Tasks

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
                                     
2012-02-21



Hardware and Software, Engineered to Work Together