String,StringBuffer,StringBuilder的区别

时间:2022-01-21 02:19:31
public static void main(String[] args) {
String str = new String("hello....");
StringBuffer stringBuffer = new StringBuffer(str);
StringBuilder stringBuilder = new StringBuilder(); stringBuffer.append("world").append("!!");
//System.out.println(stringBuffer.reverse());//字符串反转
stringBuffer.insert(, "$$");
System.out.println(stringBuffer.toString());
//stringBuffer.delete(start, end) /**
* String,StringBuffer,StringBuilder的区别:
* String类的内容一旦声明则不可改变,StringBuffer,StringBuilder的内容可以改变
* 三者都是CharSequence接口的子类
* StringBuffer是从JDK1.0时提供的,属于线程安全的操作,是同步操作,因为方法上有synchronized关键字
* StringBuilder是从JDK1.5出现的,线程不安全,是异步操作,但效率高
* 多线程访问同一资源时,必须用StringBuffer
*
**/
}

public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence

StringBuffer 的 append()
@Override
public synchronized StringBuffer append(String str) {
toStringCache = null;
super.append(str);
return this;
}

public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence

StringBuilder 的 append()
@Override
public StringBuilder append(String str) {
super.append(str);
return this;
}