toString()方法,tostring方法
分享于 点击 14439 次 点评:250
toString()方法,tostring方法
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);
相关文章
- 暂无相关文章
用户点评