Object.toString()如何获取“内存地址"?以及我该如何模仿 [英] How does Object.toString() get the "memory address" and how can I mimic it

查看:118
本文介绍了Object.toString()如何获取“内存地址"?以及我该如何模仿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ObjecttoString方法是唯一的,因为它似乎是Java中唯一可见内存地址的位置. Object如何做到这一点?

The toString method of Object is unique in that it seems to be the only place in Java where the memory address is viewable. How does Object do this?

我想知道,以便可以在自己的类中模拟它的实现.我不能使用super.toString(),因为我正在扩展一个已经覆盖toString的类.

I would like to know so that I can mimic its implementation in a class of my own. I can't use super.toString() because I'm extending a class which overrides toString already.

更新:我的问题的前提是询问内存地址,但答案表明该前提不正确,所以我实际上要问的是: Object.toString()如何返回其作用以及如何返回我可以模仿吗?

Update: The premise of my question asks for the memory address, but the answers have indicated that this premise is incorrect, so what I am actually asking is: How does Object.toString() return what it does, and how can I mimic it?

推荐答案

它不是内存地址,它是hashCode().另请参见 Object.toString() ,其中说(部分)

It is not the memory address, it is the hashCode(). See also Object.toString() which says (in part)

ObjecttoString方法返回一个字符串,该字符串由对象是其实例的类的名称,符号字符@和哈希码的无符号十六进制表示组成.物体.换句话说,此方法返回的字符串等于:

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

Object.hashCode() (通常通过将对象的内部地址转换为整数来实现,但是Java™编程语言不需要此实现技术.)因此不需要内存地址,并且如果使用该地址,则仅对Object的内部(本机)实现可见.

And Object.hashCode() says (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.) so it's not required to be a memory address, and if used it's only visible to the internal (native) implementation of Object.

这篇关于Object.toString()如何获取“内存地址"?以及我该如何模仿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆