I have the following test class in a file named Sandbox.java
:
package cas;
public final class Sandbox {
public static void main(String[] args) {
int primitive = 42;
Integer boxed = primitive;
}
}
This is something I would expect to work because of autoboxing. However, it failed to compile with the error: "Type mismatch: cannot convert from int to Integer". To isolate the test case, I put the file in a different Eclipse project, where the code is exactly identical except that package cas;
is replaced with package sandbox
. In this project, it compiled normally.
I am using compiler compliance level 1.8, with what I believe is the most up to date JDK. Since autoboxing was introduced in 1.5 if I recall correctly, there shouldn't be any issues with int –> Integer conversion, or so I thought.
I checked the Properties for both projects, and every page with the "Enable project specific settings" checkbox has it disabled. So there should be no difference between the two projects' compilation, no?
I don't understand why this code would ever give an error in a Java 1.8 environment.