Wrapper Classes and Autoboxing in Java

We know in Java everything is an object, except, primitive types. Primitive types are char, int, boolean etc. As all other things in Java are objects, it is desirable to use our primitve types as Objects. An example situation is when we want to add int to an ArrayList which only accepts objects. The answer to this problem is wrapper classes like Integer class. We can wrap our primitive types into these class objects.

Integer iVal=new Integer(123);

So that is a wrapper class. Lets understand autoboxing now.

How to increment the value of iVal by 10?

Normal Scenario

Integer iVal=new Integer(123);
int temp=iVal.intValue();
temp+=10;
ival=new Integer(temp);

Java knows this is bit of a pain so it provides you help in form of autoboxing.

Autoboxing works

Integer iVal=new Integer(123);
iVal++;