在调试Java时解密变量信息

时间:2010-02-24 00:43:40

标签: java debugging intellij-idea

我正在使用IntelliJ IDEA 8来调试某些Java,但这个问题可能适用于所有Java调试器。在变量列表中,它们显示为:

  

myVariable = {some.package.SomeClass@12345}

我很想知道课程名称后面的数字。这个数字究竟是多少?如果两个变量是被引用的相同底层对象,那么它们是否具有相同的数字?

提前致谢。

2 个答案:

答案 0 :(得分:14)

这是JVM报告的 objectId ,有关详细信息,请参阅JDWP specification

  

唯一标识中的对象   目标VM。一个特定的对象将是   仅由一个objectID标识   JDWP命令和回复   它的生命周期(或者直到objectID是   明确处置)。 ObjectID是   没有重用来识别不同的东西   对象,除非它已经明确   处置,不管是否   引用的对象一直是垃圾   集。 objectID为0表示   一个null对象。注意存在   对象ID不会阻止   对象的垃圾收集。任何   试图访问垃圾   收集的对象及其对象ID   将导致INVALID_OBJECT   错误代码。垃圾收集可以   使用DisableCollection禁用   命令,但通常不是   必须这样做。

答案 1 :(得分:-3)

看起来它只是一个直接的toString结果,当一个类没有用它自己的实现覆盖toString时生成。

如果是这种情况(您可以通过覆盖toString并查看是否获得不同的输出来检查),那么根据Object(http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html)的Java文档,toString的默认实现是:

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

Hashcodes - 你指的是什么数字 - 非常棘手。同样,从Object上的Java文档中,hashcode通过以下契约实现:

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. 

所以,简短的回答,你的数字并不意味着它是同一个对象引用。它们可能存储相同的值,但即使这不是给定的值。