views:

49

answers:

2

Hi,

I'm using IntelliJ IDEA 8 for debugging some Java, but this question could probably apply to all Java debuggers. In the list of variables, they are displayed as:

myVariable = {some.package.SomeClass@12345}

I am curious about the number that comes after the class name. What is the number exactly? Would two variables have the same number if it is the same underlying object that is being referred to?

Thanks in advance.

+3  A: 

That is objectId reported by the JVM, for details please see the JDWP specification.

Uniquely identifies an object in the target VM. A particular object will be identified by exactly one objectID in JDWP commands and replies throughout its lifetime (or until the objectID is explicitly disposed). An ObjectID is not reused to identify a different object unless it has been explicitly disposed, regardless of whether the referenced object has been garbage collected. An objectID of 0 represents a null object. Note that the existence of an object ID does not prevent the garbage collection of the object. Any attempt to access a a garbage collected object with its object ID will result in the INVALID_OBJECT error code. Garbage collection can be disabled with the DisableCollection command, but it is not usually necessary to do so.

CrazyCoder
Awesome thanks!
Jon
A: 

It looks like it's just a straight toString result as generated when a class doesn't override toString with it's own implementation.

If that's the case (which you can check by overriding toString and seeing if you get a different output), then according to the Java documentation for Object (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html) the default implementation for toString is:

 getClass().getName() + '@' + Integer.toHexString(hashCode())

Hashcodes -- what the number you're referring to -- are pretty tricky. Again, from the Java documentation on Object, hashcode is implemented with the following contract:

The general contract of hashCode is:

* Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
* If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
* It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. 

So, short answer, you the number doesn't mean it's the same object reference. They may have the same values stored, but even that's not a given.

MCory
I'm afraid this answer is not correct, it's not a hashcode.
CrazyCoder
Yeah, I got thrown off since (like I said) it looked like the default toString implementation to me.
MCory