本文介绍了当我在java中打印**这个**指针时它显示的数字是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 此程序 public class HelloWorld { public void testFunc(){ System.out .println(Class =+ this); } public static void main(String [] args){ HelloWorld hw = new HelloWorld(); System.out.println(Hello,World); hw.testFunc(); } } 给出了这个输出: Hello,World Class = HelloWorld @ 7c6768 在第二行的HelloWorld之后, @ 7c6768 是什么意思?解决方案 根据Object类中toString()方法的文档 当 getClass()。getName()+'@'+ Integer.toHexString(hashCode()) 当你在对象上调用toString()时,如果你 ovveride 如下所示,您可以获得自己的实现 @Override public String toString(){ //返回} 否则给出默认实现,您现在看到 From Object class源代码 返回对象的字符串表示形式。通常,toString方法返回一个文本表示此对象的字符串。结果应该是简洁但信息丰富的表示,便于人们阅读。建议所有子类重写此方法。 类Object的toString方法返回一个字符串,该字符串由对象为实例的类的名称组成, at符号字符`@',以及对象哈希码的无符号十六进制表示。换句话说,此方法返回一个等于以下值的字符串: getClass()。getName()+'@'+ Integer.toHexString(hashCode()) 返回:a对象的字符串表示形式。 public String toString(){ return getClass()。getName()+@+ Integer.toHexString(hashCode()); } This program public class HelloWorld{ public void testFunc(){ System.out.println("Class = "+this); } public static void main(String[] args){ HelloWorld hw = new HelloWorld(); System.out.println("Hello, World"); hw.testFunc(); }} gives me this output: Hello, WorldClass = HelloWorld@7c6768What does @7c6768 after HelloWorld in the second line mean? 解决方案 As per Docs of toString() method in Object classWhen getClass().getName() + '@' + Integer.toHexString(hashCode())When you call toString() on object ,If you ovveride like below ,you get your own implementation @Override public String toString() { //return something }Otherwise gives the default implementation,which you are seeing right nowFrom Object class Source code Returns:a string representation of the object. public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } 这篇关于当我在java中打印**这个**指针时它显示的数字是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-27 03:16