• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

autoboxing is it autocasting?

 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is autoboxing just auto casting? in which case why does it have such a confusing name?
 
Brad Dwan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brad Dwan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brad Dwan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic