tags:

views:

227

answers:

3

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.

I wonder that, when we write out.println(objName) we can see some strings as output.

  1. what is this output?
  2. 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?

alt text

+13  A: 

That is the object 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.

Brian Agnew
[email protected] do A and @ show?
uzay95
See above (edited answer)
Brian Agnew
Another 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*.
Alexandre Jasmin
Nice bit of trivia there Alexandre.
James P.
+4  A: 

That is the output of Object's "toString()" implementation. If your class overrides toString(), it will print something entirely different.

Paul Tomblin
A: 

What you are getting is the result of the toString() method of the Object class or, more precisely, the identityHashCode() as uzay95 has pointed out.

"When we create an object in java with new keyword, we are getting a memory address from the OS."

It is important to realize that everything you do in Java is handled by the Java Virtual Machine. It is the JVM that is giving this information. What actually happens in the RAM of the host operating system depends entirely on the implementation of the JRE.

James P.