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

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KOKO银角大王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值