• Post Reply Bookmark Topic Watch Topic
  • New Topic

wrappers  RSS feed

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
""
 
tushar bhasme
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
yeah... thats what i thought...
thanks for confirming Joe...
 
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!