This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
Wendy Gibbons wrote:is autoboxing just auto casting? in which case why does it have such a confusing name?
No, it works very differently from casting.
Casting is nothing more than giving the compiler a hint that a certain value should be treated as a different type. You can for example have a List (without generics - so it's a List containing Objects). If you know that there are String objects in the list, you can get an element and cast it to String:
Note that the cast does not do any kind of conversion; it's nothing more than telling the compiler "Look, you don't know this, but this List actually contains Strings, so I want you to treat the result of calling get(0) as a String".
In autoboxing, primitive values are actually converted to instances of wrapper classes (or the other way around for unboxing).
casting tells the compiler that you know that the 2 things are different (int and Integer), and that you're mitigating the risk somehow.... there is automatic unwrapping that goes on behind the scenes when Integer and int or other wrapper classes are involved.
Comparatively, this doesn't work.....
but this does....
This is without autoboxing...
IntValue() is a method that is returning an int value of the Integer object. You're not actually changing anything to anything, wrapping, unwrapping or whatnot.... you're setting the variable to equal the return value of the method.
When you do things right, people won't be sure you've done anything at all.