java 获取变量的地址,Java中变量的内存地址
Please look at the picture below.
When we create an object in java with 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?
解决方案
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 cant 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.
Please look at the picture below. When we create an object in java with 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? 解决方案 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 cant 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.