aspose file tools*
The moose likes Beginning Java and the fly likes autoboxing is it autocasting? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "autoboxing is it autocasting?" Watch "autoboxing is it autocasting?" New topic
Author

autoboxing is it autocasting?

Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

is autoboxing just auto casting? in which case why does it have such a confusing name?
Brad Dwan
Ranch Hand

Joined: Apr 22, 2010
Posts: 62
From my understanding (might I note I'm still knew to Java,) it's when primitive types are auto-'wrapped' with their object counterparts.

so for example:


So basicly the conversion from primitive to object and alike is done for you. That's my understanding anyway. I could be wrong.


-Nev
"It's about choosing your battles, not fighting somebody else's war"
Brad Dwan
Ranch Hand

Joined: Apr 22, 2010
Posts: 62
and from what i just read, its better to stick to primitives? I'd like to know why this is. Can anybody help me with that question?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13871
    
  10

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).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

thanks
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
I'm still fuzzy on this, specifically why the following is autoboxing:


I (now) know how to avoid it by using lookupValue.intValue() but why doesn't the explicit cast avoid autoboxing?

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13871
    
  10

Janeice DelVecchio wrote:

There's no auto-(un)boxing involved in this example. The method Integer.parseInt() returns a primitive int, there's no instance of class Integer involved here.

Janeice DelVecchio wrote:This is without autoboxing...

Actually, there is autoboxing in this example. Integer.parseInt() returns a primitive int, and you're assigning it to an Integer variable. The primitive int value is autoboxed into the Integer.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10



Doesn't parseint create an Integer first? I mean, it's part of the Integer class....

is there a way to see, exactly, how that works?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

parseInt does not create an Integer. On the contrary, when you create an Integer from a String it uses parseInt. You can verify by unpacking the src.zip file in your JDK folder.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brad Dwan
Ranch Hand

Joined: Apr 22, 2010
Posts: 62
Janeice DelVecchio wrote:

Doesn't parseint create an Integer first? I mean, it's part of the Integer class....

is there a way to see, exactly, how that works?


if you got netbeans you can see it by highlighting the method and view source or something. I rarely do it, and you can't edit it "read-only".
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

I got a chance to open up the source code.... very interesting.

...so parseInt() returns an int, and decode() returns an Integer, and both do the same thing.... functionally, kindof.

Fascinating. So you can use methods in the Integer class to avoid autoboxing whether you want to use primitives or objects.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: autoboxing is it autocasting?
 
Similar Threads
S2.equals(7) is false, but (S2 == 7) is true. Short S2 = new Short((short)7); Why?
ArrayList
Help me!!!!
Pure OO
Wrapper class variable in switch