Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If wrapper classes are imutable why is the value of Integer i changing?

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Integer objects themselves do not change, it's only the reference to the objects that changes.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok reference as in




now i havent used the new keyword so i dont think the new reference will be created any changes will be in the same address

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"i" is not an object - it's a reference to an object, and its value gets changed by the code. The original object ("new Integer(10)") has not changed at all. if you had kept a reference to it around, you could check that its value is the same as it was in the beginning.

And -yes- there are now two objects with different values.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
And -yes- there are now two objects with different values.



so is this two object reffering to only one reference
 
Efrain Morales
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's 1 reference, i, poiting to one object with value 12. The original object is still there, you just don't have a reference to it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic