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.
Convert Int to Integer Using the Integer. valueOf() Method in Java. This is another that we can use to convert an int to an Integer in Java. Here, we used valueOf() method of the Integer class.
In java one canâTMt assign a string value (containing an integer only) to an int variable directly or even by casting. In case of Integer we can assign string to an object of Integer type using the Integer(String) constructor or by even use parseInt(String) to convert a String literal to an int value.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With