如何获得“对象引用”? 当 toString() 和 hashCode() 被重写时,java中的对象的?
我想打印 Java 中对象的“对象引用”以进行调试。 即根据情况确保对象相同(或不同)。
问题是有问题的类继承自另一个类,该类重写了 toString() 和 hashCode() 这通常会给我 id。
示例情况: 运行多线程应用程序,我(在开发期间)想要检查所有线程是否使用资源对象的同一实例。
I would like to print the "object reference" of an object in Java for debugging purposes.
I.e. to make sure that the object is the same (or different) depending on the situation.
The problem is that the class in question inherits from another class, which has overriden both toString() and hashCode() which would usually give me the id.
Example situation:
Running a multi-threaded application, where I (during development) want to check if all the threads use the same instance of a resource object or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您到底打算用它做什么(您想要做什么与您需要调用什么有所不同)。
hashCode
< /a>,如 JavaDocs 中所定义,表示:因此,如果您使用
hashCode()
来查找如果它是内存中的唯一对象,那么这不是一个好方法。系统.identityHashCode
执行以下操作:对于您正在做的事情,听起来像是您想要的......但是您想要做的事情可能不安全,具体取决于库的实现方式。
What exactly are you planning on doing with it (what you want to do makes a difference with what you will need to call).
hashCode
, as defined in the JavaDocs, says:So if you are using
hashCode()
to find out if it is a unique object in memory that isn't a good way to do it.System.identityHashCode
does the following:Which, for what you are doing, sounds like what you want... but what you want to do might not be safe depending on how the library is implemented.
这就是我解决它的方法:
This is how I solved it:
Double equals
==
将始终根据对象标识进行检查,无论对象的 hashCode 或 equals 实现如何。 当然 - 确保您正在比较的对象引用是易失性的(在 1.5+ JVM 中)。如果您确实必须拥有原始的 Object toString 结果(尽管这不是您的示例用例的最佳解决方案),Commons Lang 库有一个方法 ObjectUtils.identityToString(Object) 它将执行您想要的操作。 来自 JavaDoc:
Double equals
==
will always check based on object identity, regardless of the objects' implementation of hashCode or equals. Of course - make sure the object references you are comparing arevolatile
(in a 1.5+ JVM).If you really must have the original Object toString result (although it's not the best solution for your example use-case), the Commons Lang library has a method ObjectUtils.identityToString(Object) that will do what you want. From the JavaDoc:
你不能安全地做你想做的事,因为默认的 hashCode() 可能不会返回地址,并且已经提到过,多个对象可能具有相同的 hashCode。 实现您想要的唯一方法是实际重写相关对象的 hashCode() 方法并保证它们都提供唯一的值。 这对于您的情况是否可行是另一个问题。
根据记录,我在 WAS 服务器中运行的 IBM VM 中遇到了具有相同默认哈希码的多个对象。 我们有一个缺陷,放入远程缓存的对象会因此被覆盖。 这让我大开眼界,因为我假设默认的哈希码也是对象的内存地址。
You cannot safely do what you want since the default hashCode() may not return the address, and has been mentioned, multiple objects with the same hashCode are possible. The only way to accomplish what you want, is to actually override the hashCode() method for the objects in question and guarantee that they all provide unique values. Whether this is feasible in your situation is another question.
For the record, I have experienced multiple objects with the same default hashcode in an IBM VM running in a WAS server. We had a defect where objects being put into a remote cache would get overwritten because of this. That was an eye opener for me at that point since I assumed the default hashcode was the objects memory address as well.
向所有实例添加唯一的 id,即
从 AbstractSomething 扩展并查询此属性。 在单个虚拟机内是安全的(假设没有使用类加载器玩游戏来绕过静态)。
Add a unique id to all your instances, i.e.
Extend from AbstractSomething and query this property. Will be safe inside a single vm (assuming no game playing with classloaders to get around statics).
我们可以简单地从对象类的 tostring 中复制代码来获取
字符串的引用
we can simply copy the code from tostring of object class to get the
reference of string