Memory address of variables in Java
Solution 1
That is the class name and System.identityHashCode() separated by the '@' character. What the identity hash code represents is implementation-specific. It often is the initial memory address of the object, but the object can be moved in memory by the VM over time. So (briefly) you can't rely on it being anything.
Getting the memory addresses of variables is meaningless within Java, since the JVM is at liberty to implement objects and move them as it seems fit (your objects may/will move around during garbage collection etc.)
Integer.toBinaryString() will give you an integer in binary form.
Solution 2
It is possible using sun.misc.Unsafe
: see this great answer from @Peter Lawrey -> Is there a way to get a reference address?
Using its code for printAddresses() :
public static void printAddresses(String label, Object... objects) {
System.out.print(label + ": 0x");
long last = 0;
int offset = unsafe.arrayBaseOffset(objects.getClass());
int scale = unsafe.arrayIndexScale(objects.getClass());
switch (scale) {
case 4:
long factor = is64bit ? 8 : 1;
final long i1 = (unsafe.getInt(objects, offset) & 0xFFFFFFFFL) * factor;
System.out.print(Long.toHexString(i1));
last = i1;
for (int i = 1; i < objects.length; i++) {
final long i2 = (unsafe.getInt(objects, offset + i * 4) & 0xFFFFFFFFL) * factor;
if (i2 > last)
System.out.print(", +" + Long.toHexString(i2 - last));
else
System.out.print(", -" + Long.toHexString( last - i2));
last = i2;
}
break;
case 8:
throw new AssertionError("Not supported");
}
System.out.println();
}
I set up this test :
//hashcode
System.out.println("Hashcode : "+myObject.hashCode());
System.out.println("Hashcode : "+System.identityHashCode(myObject));
System.out.println("Hashcode (HEX) : "+Integer.toHexString(myObject.hashCode()));
//toString
System.out.println("toString : "+String.valueOf(myObject));
printAddresses("Address", myObject);
Here is the output :
Hashcode : 125665513
Hashcode : 125665513
Hashcode (HEX) : 77d80e9
toString : java.lang.Object@77d80e9
Address: 0x7aae62270
Conclusion :
- hashcode != address
- toString = class@HEX(hashcode)
Solution 3
That is the output of Object's "toString()" implementation. If your class overrides toString(), it will print something entirely different.
Solution 4
This is not memory address
This is classname@hashcode
Which is the default implementation of Object.toString()
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
where
Class name
= full qualified name or absolute name (ie package name followed by class name)
hashcode
= hexadecimal format (System.identityHashCode(obj) or obj.hashCode() will give you hashcode in decimal format).
Hint:
The confusion root cause is that the default implementation of Object.hashCode()
use the internal address of the object into an integer
This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.
And of course, some classes can override both default implementations either for toString()
or hashCode()
If you need the default implementation value of hashcode()
for a object which overriding it,
You can use the following method System.identityHashCode(Object x)
Solution 5
Like Sunil said, this is not memory address.This is just the hashcode
To get the same @ content, you can:
If hashCode is not overridden in that class:
"@" + Integer.toHexString(obj.hashCode())
If hashCode is overridden, you get the original value with:
"@" + Integer.toHexString(System.identityHashCode(obj))
This is often confused with memory address because if you don't override hashCode(), the memory address is used to calculate the hash.
Related videos on Youtube
Comments
-
uzay95 over 2 years
Please take a look at the picture below. When we create an object in java with the
new
keyword, we are getting a memory address from the OS.When we write
out.println(objName)
we can see a "special" string as output. My questions are:- What is this output?
If it is memory address which given by OS to us:
a) How can I convert this string to binary?
b) How can I get one integer variables address?
-
phunehehe over 14 yearswell I'm not voting down because the question is clear enough, just a suggestion that you should have made it in text so people can search for it
-
Joseph almost 14 yearsUsing the sun.misc.Unsafe it is possible to get the address of a java object. For program listing refer: javapapers.com/core-java/address-of-a-java-object
-
Naveen about 9 yearsthe pointed value is hexadecimal representation of the hashcode of the object a1 & a2
-
Alex Jasmin over 14 yearsAnother interesting point is that identity hash codes are not guaranteed to be unique. For instance on 64-bit JVM there are 2^32 identity hash codes but 2^64 memory addresses.
-
Matt McHenry almost 11 yearsActually, the identity hash code cannot change, otherwise the contract of hashCode() would be violated.
-
Panduka Wedisinghe over 8 yearsin your picture a1 and a2 are two different memory addresses.that is the reason behind of getting two different values.
-
Sled over 8 yearsI'm using this for logging/debugging for determining in logs when objects are pointing to the same object instead of equivalent ones. For these purposes the
identityHashcode
is not meaningless, it's just not foolproof. :) -
Onic Team about 6 years@BrianAgnew : I want to know -> Why two object has the same hashcode. I am confused because I have learned in c or c++ every variable or object have different memory location. Then in java How can identify or differentiate two objects with the same hashCode.
-
Brian Agnew about 6 years@VedPrakash the object hashcode allows objects to be stored in hashed collections. If you want to differentiate two different objects, you could simply use reference equality
-
Onic Team about 6 years@BrianAgnew: Thanks for your reply. One question, please - In c or c++ variable and objects differentiate by the Memory address, So my question is how to diffracted by java compiler. eg when I have declared two variable in Java, String s ="abc", or again String s ="abc", then java gives compilation error duplicate variable s but some time hashcode are same in some cases. Then how to java check it. s is duplicate but hash code are same. Even two object has the same hashcode
-
Holger almost 6 years@VedPrakash as explained above, the hash code is not a memory address. When you compare two object references via
==
, the JVM implementation still may use memory addresses to tell the objects apart, but that’s an implementation detail. Hash codes may collide, but memory addresses not (at a particular point of time, when the reference comparison is made). Memory addresses may change, but still, two different objects may not occupy the same address (again, at a particular point of time). -
99Sono over 4 yearsGetting the memory address of an object is not meaningless if you try to find it out in Eclipse Memory analyzer...
-
jaco0646 about 4 yearsIdentityHashMap is an example of a class that relies on
System.identityHashCode()
. -
Paul Stelian about 3 years(probably off-topic but I'm curious) Is there a limitation of maximum 32GB for the entire heap or something?