• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to print reference value for Wrapper Object  RSS feed

 
Mark Spencer
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone please tell me how can I print reference value for a Wrapper object for example Integer (not the value)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"mahesh sl",
Please check your private messages.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're safe in assuming a question like this will not appear on the SCJP exam.

Just out of curiosity, why are you asking? Is there something you would be doing with this value?
[ February 02, 2008: Message edited by: marc weber ]
 
Mark Spencer
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I wanted to check the reference so that I can see how Primitive Wrapper objects are immutable..
Anyways I wrote code like this... is it ok to check immutability?

Integer i = 10;
Integer j = i;
System.out.println(i==j);
i++;
System.out.println(i==j);
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I wanted to check the reference so that I can see how Primitive Wrapper objects are immutable..
Anyways I wrote code like this... is it ok to check immutability?


What does the location of the object -- and whether it changes or not -- has anything to do with immutability? (but for the record, unless it is incredibly short lived, it will change)

Henry
 
Mark Spencer
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we write
Integer i = 10;
Integer j = i;

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...
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Henry
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Mark Spencer
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



Mahesh
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!