Java基础_StringBuffer
StringBuffer
StringBuffer sb =new StringBuffer(); StringBuffer sb2 =new StringBuffer(); StringBuffer sb3 =new StringBuffer(); sb.append("hello"); sb2=sb.append(",nice to meet u "); sb3=sb2.append("too"); System.out.println(sb); --->hello,nice to meet u too. System.out.println(sb2); --->hello,nice to meet u too. System.out.println(sb3); --->hello,nice to meet u too.
输出相同原因是全部指向了sb对象。
sb2和sb3全部指向了sb,那如果只是赋值该怎么办?
sb2.append(sb)
这样就完成了将sb的值赋给sb2.
将string转化程stringbuffer对象的两种方法
StringBuffer sb =new StringBuffer("abc"); StringBuffer sb2 =new StringBuffer(); sb2.append("abc");
将Stringbuffer转化为String的三种方法
StringBuffer sb =new StringBuffer("abc"); String str=sb.substring(0); //截取 String str2=new String(sb); //构造 String str3=sb.toString(); //toString方法
* StringBuffer是jdk1.0版本的,是线程安全的,效率低
* StringBuilder是jdk1.5版本的,是线程不安全的,效率高
* String和StringBuffer,StringBuilder的区别
* String是一个不可变的字符序列 * StringBuffer,StringBuilder是可变的字符序列