Boxing/Unboxing is the process of converting a primitive datatype like int, char, byte into an Object (or vice versa). In Java you have for every primitive datatype a class which basically represents it as a class e.g:
class Integer { private int value; //Methods}.
The process of extracting the value out of an object or storing such value inside a object is called unboxing/boxing.
Integer i = (Integer) 42;
int x = i.intValue();
You need this to pass a primitive datatye by reference instead of by value and to store it inside container class like ArrayList
Java also has static methods which aren't so OO.[size=78%]http://stackoverflow.com/questions/4002201/why-arent-static-methods-considered-good-oo-practice[/size]
Although you probably meant the right thing, your explanation is not exactly right.
Autoboxing and unboxing is done by the compiler and it is only done between the primitve types and their corresponding wrapper classes (int <-> Integer, double <->Double, but NOT int <->Double and NOT int <->SomeObject). It is NOT a conversion from a primitive to an object.
There are only two ways, when autoboxing or unboxing is applied by the compiler. You either assign a primitive value to the corresponding wrapper class (or vice versa for unboxing) or you pass a primitive value as parameter to a method that expects the corresponding wrapper class (or vice versa for unboxing)
Your first example is an unneccessary cast (casting != autoboxing):
Integer i = (Integer) 42;
What happens here is that the compiler does the autoboxing by applying this instead:
Integer i = (Integer) Integer.valueOf(42);
Now you see why the cast is superfluous.
Your second example is what the compiler would do from
int x = i;
Also: You can not pass by reference in Java, but you can pass references (by value)
However that's not why you need the wrapper classes.
I would like you to try the following code:
public static void main(String[] args) {
Integer val = 2;
increment(val);
System.out.println(val);
}
public static void increment(Integer i) {
i++;
}
Although a reference type is used, you can not change the value of val this way.
The reason is that an Integer is immutable (as well as the other wrapper classes).
So passing the reference is not of any use.
The only reason for wrapper classes is that you can use primitive values with generic classes and interfaces and so you can call methods on them every object has, like equals. Collections are one example you already mentioned.
This is interesting. I had heard in passing that Java "was not purely OO". Is what you're mentioning called autoboxing? For example, int char byte are not objects. Is this the right concept?
What vezzy mentioned didn't include any autoboxing. He just said, Java has types that are no object, thus it is not a pure object oriented language.
Autoboxing is just there to have cleaner, better readable code. You can perfectly live without it, but you would have to do the wrapping of primitive values explicitly all the time. So it is just a matter of convenience to have autoboxing.