aspose 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
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: 1050



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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

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: 39548
    
  27
"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 - updated with new look and Ping home screen widget
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1050

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?
 
Similar Threads
small EL question
Wrapper classes are immutable?
problem about constructing Integer objects
Polymorphic Declaration
wrapper class