Reputation: 3443
I am a complete beginner and I'm trying to learn java. I read about the concept of Autoboxing
and Unboxing
here.
I am working on java version 1.8.0_05 and using Eclipse.
The code is :
class Test {
public static void main(String[] args) {
Integer iob = 100; // shows error -> Type mismatch: Cannot convert from int to Integer
}
}
Thanks for the help.
Upvotes: 8
Views: 11442
Reputation: 31035
Autoboxing feature is available since Java 1.5.
Make sure that you are running at least Java 1.5.
Otherwise you need new Integer(100) or Integer.valueOf(100). Or just declare it as int instead of Integer.
What you can do is to check the PATH and JAVA_HOME using:
Object obj = System.getenv();
System.out.println(obj);
You should get an output like:
{JAVA_MAIN_CLASS_5612=org.eclipse.jdt.internal.junit.runner.RemoteTestRunner, SHELL=/bin/bash, TMPDIR=/var/folders/zw/n554nzl151sgtr5rqftblcjm0000gn/T/, com.apple.java.jvmMode=client, __CF_USER_TEXT_ENCODING=0x1F5:0:0, APP_ICON_5439=../Resources/sts.icns, PATH=/usr/bin:/bin:/usr/sbin:/sbin:/Users/fede/jdk1.8/bin, JAVA_HOME=/Users/fede/jdk1.8/, USER=fede, com.apple.java.jvmTask=CommandLine_Manual.java, HOME=/Users/fede, LOGNAME=fede, Apple_PubSub_Socket_Render=/tmp/launch-5UzUjs/Render, SSH_AUTH_SOCK=/tmp/launch-2okgJL/Listeners, __CHECKFIX1436934=1, JAVA_STARTED_ON_FIRST_THREAD_5439=1}
That can help figuring out what is happening
Upvotes: 0
Reputation: 8323
You need to have your language level set to at least 1.5/5.0 to take advantage of autoboxing/unboxing.
Change your settings in Project --> Properties --> Java Compiler
, chances are, it's not set to the right level.
Note this is NOT directly tied to the version of the JDK you're using, it simply means the level in which your java code will be interpreted as if it were no higher than the language level version, using any particular version of the JDK that is at least at or higher than the given language level setting.
IE: you're using JDK 1.8+, setting your language level to 5.0 means you will only be able to use java features that are up to JDK 1.5.
Upvotes: 21
Reputation: 30756
Sounds like you have the wrong language level set in Eclipse.
See @Override gives error in eclipse? :
to change the language level go To Project > Properties > Java Compiler and set the language level there. You may need to click to enable project specific settings.
Upvotes: 1