当我在java中打印**这个**指针时它显示的数字是多少? [英] what is the number that it shows when I print out the **this** pointer in 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方法返回一个由对象是实例的类的名称,at符号字符@,以及对象的哈希码的无符号十六进制表示。换句话说,此方法返回一个等于值的字符串:
当
getClass()。getName()+'@'+ Integer.toHexString(hashCode())
当你在对象上调用toString()时,如果你 ovveride 如下所示,您可以获得自己的实现
@Override
public String toString(){
//返回
}
否则给出默认实现,您现在看到
< blockquote>
返回对象的字符串表示形式。通常,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, World
Class = HelloWorld@7c6768
What does @7c6768
after HelloWorld in the second line mean?
As per Docs of toString() method in Object class
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:
When
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 now
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
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())
Returns:
a string representation of the object.
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
这篇关于当我在java中打印**这个**指针时它显示的数字是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!