JAVA学习日志–在toString方法中打印地址

在我们创建某个类的时候,我们也许希望他的toString方法能够打印出该对象的存储地址,我们也许会想到在return语句中添加this来显示地址,然而这种似乎合乎情理的方法却是错误的:

class cup {
 @Override public String toString() {
  return "address : " + this + " " it // is wrong , it will cause recursion call
  //return "address " + super.toString() + "\n" ;
 }
}

我们在打印这个类的对象的时候会发生什么呢?

Exception in thread "main" java.lang.StackOverflowError
        at java.lang.AbstractStringBuilder.append(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at Tea.toString(ArrayListDisplay.java:6)
        。。。

这只是错误提示的一段,我们会得到一个超级长的错误提示,这是因为我们使用了this,在return语句中,编译器碰到this会自动地去寻找这个导出类地toString方法,从而产生了递归调用,所以错误提示才会如此之长。

我们想要得到地址的时候,应该调用地时Object的toString方法,因为所有的类都是继承自Object类,我们只需要把this替换成super.toString()就行了。

class cup {
 @Override public String toString() {
  //return "address : " + this + " " ; //it is wrong , it will cause recursion call
  return "address " + super.toString() + "\n" ;
 }
}
C:\Users\26973\Desktop\java\chapter13\E02>java MainClass
address cup@6d06d69c

我们可以看到这是,程序可以正常地打印出地址了。
Conclusion : 想要打印地址时,在toString方法里面添加的只能是super.toString,不能时this