Please help in understanding the correlation or difference between wrapper class and wrapper implementation with a simple example. Also how is autoboxing correlated to wrapper object(Page 289 from Head First Java)
Kalyan Naveenan wrote:Hello to all JavaRanchers,
Please help in understanding the correlation or difference between wrapper class and wrapper implementation with a simple example.
It's not clear what you are asking, or what you don't understand. If you want to know how the wrapper classes are implemented, you can see for yourself. The source code is in src.zip, included with your JDK download. Otherwise, please SearchFirst for java wrapper example or java wrapper tutorial or something, and post again if you have a more specific question.
Also how is autoboxing correlated to wrapper object(Page 289 from Head First Java)
I don't have that book, but autoboxing is correlated to wrapper classes in that it provides automatic conversion between wrappers and their corresponding primitives. Again, you can find more details with a quick web search, and then, if you still have questions, for a more detailed answer here, post more detailed question.
Most types in Java are classes. But there are exceptions - primitives like double and int - and this decision was made for efficiency reasons. But there are times when you need an instance of a class - for instance, the collection classes can only contain objects, not primitives. So wrapper classes were designed for those case when you need an object.
Which means this sort of thing was common:
(in fact it was even worse, because generics didn't exist either so you had to cast an Object to an Integer, but I'll ignore that for now).
Auto-boxing means the compiler will automatically convert from a primitive to a wrapper for you (and auto-unboxing means it will also go the other way). So now your code doesn't need to be littered with these conversions: