• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Wrapper Class and ==

 
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why is the output true, false? The wrapper classes don't seem to behave like regular objects.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They do. But they're immutable. When you have an Integer and use the ++ operator, this is happening:

- The Integer is auto-unboxed to an int
- The int is incremented
- The int is auto-boxed to a new Integer

So by the end of that code, a and b are pointing at different objects.
 
Cole Tarbet
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just like String... Can't rely on == for comparisons. Thanks!
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cole Tarbet wrote:Just like String... Can't rely on == for comparisons. Thanks!



Well, wrappers are after all a subtype of object are they not instanceof!
 
Cole Tarbet
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scotty Mitchell wrote:Well, wrappers are after all a subtype of object are they not instanceof!



I don't understand what you mean...?

The reason behind my question is that if I used StringBuilder and append then the output would be true, true because both references refer to the same object. I can understand int being immutable, but it would make more sense if Integer behaved like a regular object.
 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyway, what I was thinking is that whenever you arnt looking at non primitives i.e an object of some sort == isnt checking for meaningful equality. Or am I wrong to think this?

Also, StringBuilder is mutable is it not? The immutable thing is pretty important here...isnt that why theirs new object creation?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is acting like a regular object. The think that's confusing you is the auto-boxing/unboxing, which was added to the language much later than the wrapper classes. In old-style Java:
Would be replaced by:
At which point, you probably wouldn't be surprised that aand b are referencing different objects;

 
Cole Tarbet
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Matthew - That does make it more clear. The special case here is autoboxing.

Scotty Mitchell wrote:Anyway, what I was thinking is that whenever you arnt looking at non primitives i.e an object of some sort == isnt checking for meaningful equality. Or am I wrong to think this?

Also, StringBuilder is mutable is it not? The immutable thing is pretty important here...isnt that why theirs new object creation?



== checks if objects are the exact same instance in memory. It might be useful to make that distinction in some special case.

StringBuilder is mutable.
 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

== checks if objects are the exact same instance in memory. It might be useful to make that distinction in some special case.



Yes, I do agree it would be helpful to make that distinction in some case. What I meant by meaningful equality was meant towards referencing if an objects state values are the same.

Open question:

Is unboxing of Integer why the below code prints out true or is that int 1 is being boxed to Integer? I feel as though if it were latter case the result would mean they would not be equal.

Also since string doesnt have a boxing feature so the false should print out in this case because the string objects are pointing to different references correct?


 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic