Why compiler doesn’t give error when we assign Integer(object) to int(primitive)?
int i;
Integer ii = new Integer(5);
i = ii;//no compilation error.
And this is the case with all other types(byte-Byte, float-Float)..
What is the reason? Am i missing something here?
It’s called autoboxing/unboxing.
As of Java 1.5, the compiler automatically “boxes” primitives into their corresponding class (eg
intandInteger,doubleandDoubleetc), and un-boxes as required.See this page in the documentation for more details.