• 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

wrappers

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

As per my understanding, wrappers are nothing but Objects. So here is what i tried:
Integer i = new Integer(3);
Integer j = i;
j=j++;
syso(i);

I was expecting since j is referencing the same object as that of i, any change in j would reflect in i... but it dsnt...
does it have to do with auto boxing and unboxing or smthing?
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try printing out both J and I? What values do you get for both?
 
tushar bhasme
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
j prints the expected value of 4 while i iprints 3...
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post your whole code? When I copy what you have there and print it, I get both values as 3.
 
tushar bhasme
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
""
 
tushar bhasme
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


you are correct... using j=j++ will result in no change in the value of j and it will stay as 3...
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, in your original code you had j=j++. Anyway, what I think is happening is when you do j++ the Integer is unboxed to an int, incremented, then boxed to an Integer again, which is a shiny new Integer, not the original you had assigned j to. i would remain the same, and j would be incremented by 1.
 
tushar bhasme
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah... thats what i thought...
thanks for confirming Joe...
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tushar bhasme wrote:
I was expecting since j is referencing the same object as that of i, any change in j would reflect in i... but it dsnt...
does it have to do with auto boxing and unboxing or smthing?



Wrapper object are immutable, so you can't change the value once you've created it. What does happen here, it creates a new object and assign it to j.

try this,
 
reply
    Bookmark Topic Watch Topic
  • New Topic