c.toString() 和 String s = new String(c) 区别,c.tostringstring
分享于 点击 2907 次 点评:286
c.toString() 和 String s = new String(c) 区别,c.tostringstring
String str = "abcd";
char [] c = str.toCharArray();
String s = new String(c);
String s2 = c.toString();
其中s和s2有什么区别???
String str = "abcd"; char [] c = str.toCharArray(); String s = new String(c); // 由char数组构建一个String对象 String s2 = c.toString(); // 将对象c的toString结果(一个String对象)赋给s2对象 s和s2都是String对象,他们的创建方式不同 s值是 "abcd" s2值是对象c的hascode,因为toStrng方法默认返回当前对象(c)的内存地址,即hashCode
延伸:
StringBuilder sb =new StringBuilder();
sb.toString();
显示的是正常的 ,不是内存地址
相关文章
- 暂无相关文章
用户点评