• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

autoboxing is it autocasting?

 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is autoboxing just auto casting? in which case why does it have such a confusing name?
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic