So you have a couple of different concepts involved there.
The first is "auto-boxing". Do you understand that? Because you can't use primitives where you need an Object (such as in collections) all primitive types have a corresponding wrapper class. So int has Integer, double has Double, etc.
Which means that Java code used to be full of conversions between primitives and wrappers.
(in fact, it was worse than that, because you didn't have generics either, so there was an added cast to Integer).
Auto-boxing was added to the language to make life easier - these conversions can be carried out automatically:
So if you have a method that takes an Integer argument, you can call it with an int argument and the auto-boxing takes care of the rest.
The second thing going on with your code is that an Integer is an Object. Which means that if you have a method that takes an Object argument, you can always give it an Integer argument instead (the "substitution principle"). Put these together, and you can also call it with an int argument. This will "box then widen" - convert an int to an Integer, then use that as an Object.
One thing to remember, though, is that you cannot "widen then box". For example, if you tried this:
That won't work. The int can be boxed to an Integer, but then that can't isn't compatible with a Long. And the compiler will not convert the int to a long and then box that to a Long ("widen then box"). So that won't compile.
This is an autoboxing and relates only to wrapper objects. So the java itself will provide necessary changes. I suppose these is a compiler who will insert necessary byte-code into your program at compile time. When you pass a primitive to your method, and object of type 'Integer' will be created (since the default for numerics is int). This object will be passed to the method. And next step is a simple reference casting, implicit in our case.
Roel De Nijs wrote:So I'll just stick to this (administrative) note: autoboxing/unboxing is not on the OCAJP7 exam (it is a topic for the more advanced OCPJP7 exam).
Questions about it aren't. But questions containing code like this are:
list.add(4);
In other words, reading code that uses autoboxing could come up. But you wouldn't need to know anything subtle about it.
Thank you Matthew for detailed explanation and for one Extra point you teach me ,
The int can be boxed to an Integer, but then that can't isn't compatible with a Long. And the compiler will not convert the int to a long and then box that to a Long ("widen then box"). So that won't compile.
Thank you Sergei Zhylinski for one catchy point as
(since the default for numerics is int)
i 'll experiment on this.
Roel ...I spend a half day on this and come across your post
So I'll just stick to this (administrative) note: autoboxing/unboxing is not on the OCAJP7 exam (it is a topic for the more advanced OCPJP7 exam).
.
Thanks Roel for keeping eye on each small topic we all guys post here and its relevance with Exam Objectives.
Its better just to be aware of autoboxing/unboxing.