Originally posted by mahesh sl: Hi, Can anyone please tell me how can I print reference value for a Wrapper object for example Integer (not the value)
If by reference value, you mean the address where the object is located, there is no way to do this in pure Java. You could however, write JNI code to do this -- but since objects move in the heap, the value may not be valid for very long.
Henry [ February 02, 2008: Message edited by: Henry Wong ]
the reference value will be same for i and j. and after i++, reference value for i will be changed keeping the reference value for j intact. isn't it ?? And that means object created initially still there and we can say its immutable. I thought its best way to check immutability for object. Please, correct me if I am wrong...
And that means object created initially still there and we can say its immutable. I thought its best way to check immutability for object.
No... Just because an object reference happens to still be pointing to the original object, has nothing to do with immutability. If you assign i to something else, does that mean that Integer is no longer immutable?
Originally posted by Mahesh Langote: ...after i++, reference value for i will be changed keeping the reference value for j intact. isn't it ?? And that means object created initially still there and we can say its immutable...
A lot of people just ask for "answers," so I think it's great that you are exploring these concepts by writing your own test code! The confusion seems to be about immutability and references.
An "immutable" object is one in which the state cannot be changed after it's created. In the case of wrappers, this means that the object is created to wrap a particular value, and this value cannot be changed after the object is created.
A "reference" is like an address telling where an object can be found. But as Henry has pointed out, the actual location of a object can change depending on how memory is managed. This has nothing to do with immutability, because the state of the object does not change when it gets moved around in memory.
In Java, we really don't care where an object is. We only care that it can be reached via its reference.
Now, your code has a lot going on behind the scenes, and I'm not sure it's telling you what you think it's telling you. Consider that when you call i++, the Integer referenced by i is automatically unboxed to an int, then the int value is incremented, then the incremented value is boxed an Integer, and then a reference to the boxed Integer is assigned to the variable i. (See JLS 15.14.2.) The boxing creates a new Integer instance -- unless the value is within the range of a byte and there is already a boxed (not "new") Integer wrapping that value. (See JLS 5.1.7.)
With that in mind, consider the following code...
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
posted 10 years ago
Thanks Henry and thanks marc,
For your explanations. Now I understood what immutable means. Its regarding object created in Heap. And yes one more thing, thanks again to both of you , I really didn't knew that object location can change in course of program run.