如何获取Java对象的地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1360826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get address of a Java Object?
提问by Iguramu
Is there a way to get address of a Java object?
有没有办法获取Java对象的地址?
Where the question comes from?: At First, I read properties file and all the data from file was placed into table. Properties file can update. So, I want to listen that file. I listen an object using PropertyChangeSupport and PropertyChangeListener.
问题来自哪里?:首先,我读取属性文件并将文件中的所有数据放入表中。属性文件可以更新。所以,我想听那个文件。我使用 PropertyChangeSupport 和 PropertyChangeListener 监听对象。
updatedStatus = new basit.data.MyString();
updatedStatus.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
//After changes "i", we inform the table model about new value
public void propertyChange(PropertyChangeEvent evt) {
Object objec=evt.getNewValue();
tableModel.setValueAt(objec.toString(), 0, 5);
}
});
If updatedStatus changes then i update table. MyString class have private String "Value". I want to listen properties file. So, it should make updatedStatus.value and String of Properties File equal at the same address. If i can do it, so i don't need to listen properties file.
如果 updatedStatus 更改,则我更新表。MyString 类具有私有字符串“值”。我想听属性文件。因此,它应该使 updatedStatus.value 和 String of Properties File 在同一地址处相等。如果我能做到,那么我就不需要听属性文件了。
updatedStatus.setValue(resourceMap.getString("HDI.Device.1.Name"));
I tried to use StringBuffer, but i couldn't achieve it. That's why, I asked the question.
我尝试使用 StringBuffer,但无法实现。这就是为什么,我问了这个问题。
采纳答案by Jon Skeet
Firstly - no, you can't get the address of an object in Java; at least, not pure Java with no debugging agent etc. The address can move over time, for one thing. You don't need it.
首先 - 不,您无法在 Java 中获取对象的地址;至少,不是没有调试代理等的纯 Java。一方面,地址可以随时间移动。你不需要它。
Secondly, it's slightly hard to follow your explanation but you certainly won'tbe able to get away without listening for changes to the file itself. Once you've loaded the file into a Properties
object, any later changes to the file on disk won'tbe visible in that object unless you specifically reload it.
其次,遵循您的解释有点困难,但如果不听取文件本身的更改,您肯定无法逃脱。一旦您将文件加载到Properties
对象中,除非您专门重新加载它,否则对磁盘上文件的任何后续更改都不会在该对象中可见。
Basically you should listen for changes to the file (or poll it) and reload the file (either into a new Properties
or overwriting the existing one) at that point. Quite whether you alsoneed to listen for updates on the string container will depend on your application.
基本上,您应该监听文件的更改(或轮询它)并在那时重新加载文件(要么加载到新文件中,要么Properties
覆盖现有文件)。您是否还需要监听字符串容器的更新取决于您的应用程序。
回答by Jeroen van Bergen
回答by mfx
System.identityHashCode(obj)
delivers the next-best thing: a number unique for each object. It corresponds to the default Object.hashCode()
implementation.
System.identityHashCode(obj)
提供次佳的东西:每个对象的唯一编号。它对应于默认Object.hashCode()
实现。
To quote the API: "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.)".
引用 API:“在合理可行的情况下,类 Object 定义的 hashCode 方法确实为不同的对象返回不同的整数。(这通常是通过将对象的内部地址转换为整数来实现的,但这种实现技术是JavaTM 编程语言不需要。)”。
回答by fero46
The best way to observe if some file changes is IMHO to make a hash value with sha1 or mda5 and save the value in a cache. And you make a Thread that every minutes, seconds, depends how often you watch file changes, and make hash value over the file. So you can compare this two values and if the values are not equivalent so you can reload the new file.
观察某些文件是否更改的最佳方法是恕我直言,使用 sha1 或 mda5 创建哈希值并将该值保存在缓存中。并且您创建一个线程,每分钟、几秒取决于您查看文件更改的频率,并在文件上生成哈希值。因此,您可以比较这两个值,如果这些值不相等,则可以重新加载新文件。
回答by hqt
Java not like C/C++. in C++, you will often work with address (that C++ programmer has a concept call pointer). But, I afraid that not in Java. Java is very safe that prevent you to touch its address.
Java 不像 C/C++。在 C++ 中,您经常会使用地址(C++ 程序员有一个概念调用指针)。但是,恐怕不是在 Java 中。Java 非常安全,可以防止您触摸其地址。
But, there other ways maybe same with your idea is use HashCode. HashCode of an object base on their address on HEAP.
但是,还有其他可能与您的想法相同的方法是使用 HashCode。对象的 HashCode 基于其在 HEAP 上的地址。
回答by Semih Okan Pehlivan
we can get address of an object in memory. Well how? it is like that;
我们可以在内存中获取一个对象的地址。怎么样?就是这样;
using sun.misc.Unsafe class in java.
在 java 中使用 sun.misc.Unsafe 类。
create new Unsafe object and use the getAddress(Object)
; method and it will return a long value that is address.
创建新的 Unsafe 对象并使用getAddress(Object)
; 方法,它将返回一个长值,即地址。
and also there are many methods for this class.
并且这个类还有很多方法。
you can change the values in this address using putInt(Object,long offset, int value)
or like this method.(getting some value getnt(Object)
).
您可以使用putInt(Object,long offset, int value)
或喜欢此方法来更改此地址中的值。(获取一些值getnt(Object)
)。
Note: this class is really UNSAFE. if you make wrong things on your project, JVMwill be stopped.
注意:这个类真的是UNSAFE。如果您在项目中做错了事情,JVM将被停止。