java - Java的对象实例如何唯一区分

标签 java jvmti

我目前正在为 Java 1.7 构建 native JVMTI 代理。问题是我需要索引一些关于特定 Java 对象实例的数据。所以我的问题是我可以使用 jobject 类型的值作为对象的实例 ID 来检索我的索引数据吗?

我已经在寻找关于什么是 jobject 类型的语义的任何信息。它是对象内存位置的指针吗?它是堆栈指针地址吗?它是 JVM 内部结构的地址吗?所以我无法确定 jobject 的值在 Java 对象的生命周期中是否是唯一且不可变的。

感谢您的帮助。

编辑

根据JNI的规范找到here , jobject 似乎是指向 Object 实例的指针。

最佳答案

当您说“jobject 类型的值”时,我猜您指的是 toString 返回的值.如果您查看 java 文档,它指出:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

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

如果您查看 hashCode 的 Java 文档它指出的方法:

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

还有

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

更新:对 Ryan 评论的回应:System.identityHashCode即使 hashcode 方法被覆盖,也会为您提供原始哈希码。然而,就像评论一样,它并不是真正独特的。

所以我猜你的问题的答案是肯定的,它是不可变的,而且它很可能是独一无二的,但你应该阅读你的 JVM 的文档或源代码。

关于java - Java的对象实例如何唯一区分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9637526/

相关文章:

java - 如何修复设置 xml 中无法识别的标签?

java - 解决 JNI DefineClass 中的依赖关系

java - 使用 Java 或 JVMTI 识别当前 JVM

java - 如何使用 JVMTI 获取 java 对象的内存地址(不是哈希码)

java - ThreadInfo 类提供的信息是否比 JVMTI 提供的信息更多?

java - 使用 JVMTI 测量 Java 的执行时间

java - MIDI 程序警告

java - 从 Java 中的 arrayList 中删除最后 n 个元素

java - 实现授权 Controller

java - 如何使 JavaFX TreeView 和 TreeItem 可序列化?