使用另一个对象的 toString 结果构造一个 new Object()

发布于 2024-12-27 12:11:34 字数 676 浏览 0 评论 0原文

我想知道是否可以获取对象的某些字符串值,以通过该特定字符串访问同一台计算机(相同 RAM)或同一虚拟机上的该对象。

例如,

Object objA1 = new Object();
System.out.print(objA1.adress); => output: d146a6581ed9e
Object objExt = Object.buildFromMemoryAdress("d146a6581ed9e");

我希望你能理解我想要理解的内容。

编辑:我发现

http://javapapers.com /core-java/address-of-a-java-object/#&slider1=1

一个类,它允许我获取(VM?)内存上实例的逻辑地址的字符串: sun.misc.Unsafe

我想我还可以使用 Unsafe 从(仅限于虚拟机?)内存中检索对象。

如果不可能这样,我会怎么做,因为出于好奇,是否有其他语言(尤其是高端语言)允许像这样直接内存访问?

I was wondering if it was possible to get some String value of an Object to access that Object on the same machine (same RAM) or the same VM via that particular String.

e.g.

Object objA1 = new Object();
System.out.print(objA1.adress); => output: d146a6581ed9e
Object objExt = Object.buildFromMemoryAdress("d146a6581ed9e");

I hope you understand what I'm trying to understand.

EDIT: I found in

http://javapapers.com/core-java/address-of-a-java-object/#&slider1=1

a Class that allows me to get the String of the logical address of an instance on the (VM?) memory: sun.misc.Unsafe

I think I can also use Unsafe to retrieve an Object from the (restricted to the VM?) memory.

If not possible like this, how would I do it, and since it's out of curiosity are there any other languages (especially high end) that allow direct memory access like this?

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

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

发布评论

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

评论(3

寻梦旅人 2025-01-03 12:11:34

假设在 toString() 结果中看到的数字是内存地址是不正确的。

事实上,它是对象的哈希码。如果对象没有被修改,它的哈希码保持不变。然而,它的内存地址可以随时改变:压缩垃圾收集器可以决定在任何需要的时候移动内存中的对象。

It is incorrect to assume that the number that you see in the toString() result is the memory address.

It is, in fact, the object's hash code. If the object isn't modified, its hash code remains constant. However, its memory address can change at any time: a compacting garbage collector can decide to move the object in memory whenever it feels like it.

流年已逝 2025-01-03 12:11:34

绝对不是。事实上,这显然是不可能的,因为显然可以有两个不同的对象,而它们的 toString() 方法返回相同的字符串。举一个简单的例子:

Integer a = new Integer(10);
Integer b = new Integer(10);

Object x = Object.buildFromToString("10");

x 应该指什么? a 引用的是同一个对象,还是 b 引用的是同一个对象?

toString() 并不是要返回对象标识符 - 它只是要返回对象的某种文本表示形式。仅仅因为默认实现返回看起来有点像标识符的东西,就不应该将其视为应该将其用作标识符的指示。

如果您想存储在其他时间点访问对象的某种方式,我建议您将对其的引用存储为 Object 变量。

Absolutely not. In fact, it's clearly impossible, given that you can obviously have two different objects whose toString() methods return the same string. As a simple example:

Integer a = new Integer(10);
Integer b = new Integer(10);

Object x = Object.buildFromToString("10");

What should x refer to? The same object that a refers to, or the same object that b refers to?

toString() is not meant to return an object identifier - it's just meant to return some sort of textual representation of an object. Just because the default implementation returns something which looks a bit like an identifier shouldn't be taken as an indication that it should be used as an identifier.

If you want to store some way of accessing an object at some other point in time, I suggest you just store a reference to it as an Object variable.

似梦非梦 2025-01-03 12:11:34

不,这是不可能的。仅当您拥有对 Java 对象的引用时才可以访问这些对象。您可以做的是将对象存储在给定名称下的 Map 中,并使用映射从其名称中获取对象的引用。

No, this is not possible. Java objects are only accessible if you have a reference to those objects. What you can do is store your objects in a Map<String, Object> under a given name, and get back the reference of the object from its name, using the map.

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