ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
![](https://box.kancloud.cn/ccb201179584011b27d8ae138edef91a_737x435.png) 证明存在toString() ~~~ package obj.a5; public class Test { public static void main(String[] args){ String name =new String("你好"); System.out.println(name.toString()); } } ~~~ 自定义的类没有重写toString()时,输出toString()会输出内存地址 ~~~ package obj.a5; public class Test { public static void main(String[] args){ //String name =new String("你好"); Cat one=new Cat(); System.out.println(one); } } class Cat{ } ~~~ ![](https://box.kancloud.cn/871d97cc9e337770d0d456ab42689bf3_279x65.png) 重写toString() ~~~ package obj.a5; public class Test { public static void main(String[] args){ //String name =new String("你好"); Cat one=new Cat(); System.out.println(one); } } class Cat{ String name; int age; public String toString(){ return "我的姓名是:"+name+"我的年龄是:"+age; } } ~~~ ![](https://box.kancloud.cn/7d6bbd4aa51ac649a914a18cb175baee_310x58.png)