Reputation: 96391
If Integer i = 88
, what is the memory address i
resides at?
Additionally, if as part of Debugger, id
number changes, is it indicative of the fact that a new object was created?
Upvotes: 2
Views: 197
Reputation: 37
Unless you have some one very close from JVM development team you cannot find out what type of data/address is stored in reference variable :) . All we need is access to the object residing in that reference and manipulate/execute it.
i am not sure about the second part of your question. Also, i cannot confirm if ID (in debugger) refers to java heap memory location.
Upvotes: 0
Reputation: 16364
The answer to your second question is yes, the "id" you see in Eclipse's debugger uniquely identifies an object and so a change in it means a new object was created. So, for example:
Integer i = 55;
Integer j = new Integer(55);
Integer k = Integer.valueOf(55);
Here i and k will have the same id, since they refer to the same object; j will be different.
The memory address of one of these objects is not really a useful piece of information, since objects move around in memory. All you can know is that if i == k, they are the same object. (So here, i == k but i != j).
Upvotes: 2
Reputation: 340733
Why would you need a memory address of any object in Java? All you can say is that Integer
instance lies in heap. You cannot find the exact address.
Second question: yes, Integer
objects are immutable, so when the value of a variable of type Integer
changes it means it points to a different instance of Integer
. However it doesn't necessarily mean a new instance was created. new Integer(42)
always creates a new instance while Integer.valueOf(42)
will create the same instance every time it is called.
Upvotes: 6