当我在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()方法的Docs

类Object的toString方法返回一个字符串,该字符串由对象为实例的类的名称,符号字符“@”以及对象的哈希码的无符号hex表示组成。 换句话说,此方法返回一个等于值的字符串:

什么时候

  getClass().getName() + '@' + Integer.toHexString(hashCode()) 

当你在对象上调用toString()时,如果你像下面那样,你会得到自己的实现

  @Override public String toString() { //return something } 

否则提供您现在看到的默认实现

来自Object类源代码

返回对象的字符串表示forms。 通常,toString方法返回一个“文本表示”此对象的字符串。 结果应该是简洁但信息丰富的表示,便于人们阅读。 建议所有子类都覆盖此方法。

类Object的toString方法返回一个字符串,该字符串由对象为实例的类的名称,符号字符“@”以及对象的哈希码的无符号hex表示组成。 换句话说,此方法返回一个等于以下值的字符串:getClass()。getName()+’@’+ Integer.toHexString(hashCode())

 Returns: a string representation of the object. public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } 

Object的toString()实现如下:

 public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } 

由于您的HelloWorld类不会覆盖它,因此这是调用的方法。

toString()方法返回对象的字符串表示forms。

通常, toString()方法返回一个“文本表示”此对象的字符串。 结果应该是简洁但信息丰富的表示,便于人们阅读。 建议所有子类都覆盖此方法。

class ObjecttoString方法返回一个字符串,该字符串由对象为实例的类的名称,符号字符“@”以及对象的哈希码的无符号hex表示组成。 换句话说,此方法返回一个等于值的字符串:

  getClass().getName() + '@' + Integer.toHexString(hashCode()) 

来自API:

类Object的toString方法返回一个字符串,该字符串由对象为实例的类的名称,符号字符“@”和对象的哈希码的无符号hex表示组成。

如果在Object类中看到toString()方法

 /** * Returns a string representation of the object. In general, the * {@code 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 {@code toString} method for class {@code Object} * returns a string consisting of the name of the class of which the * object is an instance, the at-sign character `{@code @}', 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()) * 

* * @return a string representation of the object. */ public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }

它返回Class name后跟其哈希码。 这是你得到的数字。

HelloWorld @ 7c6768是当前对象的字符串表示,@ 7c6768是哈希码。 实际上你正在调用当前对象的toString()

这是toString()的java文档http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#toString()

那就是this.hashCode() 。 由于您不重新定义hashCode() ,因此该数字是存储对象的JVM中的内存地址。

查看Objects的toString()方法:

  public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } 

它是对象的哈希值。

唯一标识对象的编号。 它是哈希码的hex表示。 简单来说,打印的整个String是实例化类后返回的引用。