wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes If wrapper classes are imutable why is the value of Integer i changing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If wrapper classes are imutable why is the value of Integer i changing?" Watch "If wrapper classes are imutable why is the value of Integer i changing?" New topic
Author

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

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055



http://www.lifesbizzare.blogspot.com || OCJP:81%
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

The Integer objects themselves do not change, it's only the reference to the objects that changes.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

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
Marshal

Joined: Mar 22, 2005
Posts: 42364
    
  64
"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.


Ping & DNS - my free Android networking tools app
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

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

Joined: Oct 14, 2009
Posts: 12
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If wrapper classes are imutable why is the value of Integer i changing?