toString()方法

来源:互联网 发布:java初级中级高级区别 编辑:程序博客网 时间:2024/05/21 06:00

1、默认从Object类继承的toString()方法返回的是
getClass().getName() + ‘@’ + Integer.toHexString(hashCode())
即该对象的类名称@该对象hashcode哈希码的无符号十六进制表示

2、在进行String与其它类型数据的连接操作时,自动调用toString()方法

Date now=new Date();System.out.println(“now=”+now);//相当于System.out.println(“now=”+now.toString());  

3、可以根据需要在用户自定义类型中重写toString()方法
如String 类重写了toString()方法,返回字符串的值。

s1=“hello”;System.out.println(s1);//相当于System.out.println(s1.toString());

4、基本类型数据转换为String类型时,调用了对应包装类的toString()方法

int a=10;System.out.println(“a=”+a);
0 0
原创粉丝点击