10

When using Intellij's debugger, the variables in scope are displayed using a 4 digit identifier, marked in red in the following screenshot. intellij screenshot

This identifier seems to be calculated based on the object's identity.

What exact code is used to get the 4 digit number for a given object instance?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Stefan Armbruster
  • 39,465
  • 6
  • 87
  • 97
  • Sure that's not just the Hex representation of the object's hashCode() -- exactly the way normal Java does it? – tim_yates Nov 18 '11 at 10:05
  • It's definitely not a hex representation since the 4 digits are always [0-9]{4} and no [abcdef]. And I assume it does not rely on hashCode() since you can override hashCode in your code. More reasonable would be something like System.identityHashCode() or the memory address - exactly that's what I wan't to know. – Stefan Armbruster Nov 18 '11 at 10:39
  • 2
    You're right... This any use? http://stackoverflow.com/q/2322903/6509 – tim_yates Nov 18 '11 at 10:50
  • That was the answer - but didn't help since the debugger's objectId is not accessible by the application to be debugged itself. – Stefan Armbruster Nov 18 '11 at 14:02
  • You should clarify the question and specify what you want to do with this ID and why you need to calculate it inside your application. Otherwise this question should be closed as an exact duplicate of http://stackoverflow.com/q/2322903/6509. – CrazyCoder Nov 18 '11 at 16:31
  • Getting this programaticaly would have helped me debugging something in which I wanted my app to generate a trace, while debugging, and comparing that trace to the debugger. – Pablo Fernandez May 08 '18 at 09:34
  • It is definitely not hex, and it is definitely unrelated to `System.identityHashCode()`. – toolforger Oct 14 '22 at 08:33

1 Answers1

4

I don't think you should rely on that id being calculated in any special way. It's internal to IDEA (or JVM) and I don't think it holds any relevance except to track objects during execution.

However, I find it useful to name objects during debug. I believe the shortcut is F11 (click once on the object in debug window first) and just give it a name that is meaningful for you during debugging. That object will always hold that name during the current session of debugging.

Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78
  • 1
    This doesn't expose the object name or value to the code, so, it only helps while looking at things in the debugger. For example, I wanted to print the id of the object at various points in my app and compare to the debugger. – Pablo Fernandez May 08 '18 at 09:35