如何获取Java对象的地址?

发布于 2024-08-03 10:22:01 字数 858 浏览 5 评论 0原文

有没有办法获取Java对象的地址?

问题从何而来?: 首先,我读取属性文件,并将文件中的所有数据放入表中。属性文件可以更新。所以,我想听一下那个文件。我使用 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);
            }
        });

如果 updateStatus 发生变化,那么我会更新表。 MyString 类有私有字符串“Value”。我想听属性文件。因此,它应该使 UpdatedStatus.value 和属性文件的字符串在同一地址处相等。如果我能做到,那么我就不需要监听属性文件。

updatedStatus.setValue(resourceMap.getString("HDI.Device.1.Name"));

我尝试使用 StringBuffer,但无法实现。这就是为什么,我问这个问题。

Is there a way to get address of a Java object?

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.

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.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猥琐帝 2024-08-10 10:22:01

首先 - 不,你无法在 Java 中获取对象的地址;至少,不是没有调试代理等的纯 Java。一方面,地址可以随着时间的推移而移动。你不需要它。

其次,理解你的解释有点困难,但如果不监听文件本身的更改,你肯定不会能够逃脱。将文件加载到 Properties 对象后,以后对磁盘上的文件所做的任何更改都不会在该对象中可见,除非您专门重新加载它。

基本上,您应该监听文件的更改(或轮询它)并在此时重新加载文件(进入新的属性或覆盖现有的属性)。您是否还需要侦听字符串容器上的更新将取决于您的应用程序。

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.

Secondly, it's slightly hard to follow your explanation but you certainly won't be 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't be visible in that object unless you specifically reload it.

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 also need to listen for updates on the string container will depend on your application.

单调的奢华 2024-08-10 10:22:01

System.identityHashCode(obj) 提供了次佳的效果:每个对象都有一个唯一的数字。它对应于默认的 Object.hashCode() 实现。

引用 API 的话:“只要合理实用,Object 类定义的 hashCode 方法确实会为不同的对象返回不同的整数。(这通常是通过将对象的内部地址转换为整数来实现的,但这种实现技术是JavaTM 编程语言不需要。)”。

System.identityHashCode(obj) delivers the next-best thing: a number unique for each object. It corresponds to the default Object.hashCode() implementation.

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.)".

╄→承喏 2024-08-10 10:22:01

我们可以得到一个对象在内存中的地址。嗯,怎么样?就是这样;

在java中使用sun.misc.Unsafe类。

创建新的 Unsafe 对象并使用 getAddress(Object) ;方法,它将返回一个长整型值,即地址。

这个类还有很多方法。

您可以使用 putInt(Object,long offset, int value) 或类似的方法更改此地址中的值(获取一些值 getnt(Object))。

注意:这个类确实是不安全。如果你在项目中犯了错误,JVM将被停止。

we can get address of an object in memory. Well how? it is like that;

using sun.misc.Unsafe class in java.

create new Unsafe object and use the getAddress(Object); method and it will return a long value that is address.

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)).

Note: this class is really UNSAFE . if you make wrong things on your project, JVM will be stopped.

查看 Apache Commons 配置。该库支持动态重新加载(例如)属性文件。请参阅此处

Look into Apache Commons Configuration. This library has support for dynamic reloading of (for example) property files. See here.

极致的悲 2024-08-10 10:22:01

观察某些文件是否发生更改的最佳方法是使用 sha1 或 mda5 生成哈希值并将该值保存在缓存中。你创建一个线程,每分钟、每秒取决于你观察文件更改的频率,并对文件进行哈希值。因此,您可以比较这两个值,如果值不相等,则可以重新加载新文件。

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.

朦胧时间 2024-08-10 10:22:01

Java 不像 C/C++。在 C++ 中,您经常会使用地址(C++ 程序员有一个概念调用指针)。但是,恐怕在 Java 中不行。 Java 非常安全,可以防止你触及它的地址。

但是,还有其他方法可能与您的想法相同,那就是使用 HashCode。对象的哈希码基于其在堆上的地址。

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.

But, there other ways maybe same with your idea is use HashCode. HashCode of an object base on their address on HEAP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文