File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Elasticsearch in Action this week in the Big Data 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: 1057



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

Joined: Oct 27, 2005
Posts: 19911
    
  23

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: 1057

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

Joined: Mar 22, 2005
Posts: 42954
    
  73
"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

Joined: Aug 01, 2009
Posts: 1057

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.
 
 
subject: If wrapper classes are imutable why is the value of Integer i changing?