相同之处:都是基于char[]数组实现不同之处: String 在常量池中或堆中 StringBuffer 是线程安全,append 方法被定义为synchronized 效率较低 /** * Appends the specified <tt>StringBuffer</tt> to this sequence. * <p> * The characters of the <tt>StringBuffer</tt> argument are appended, * in order, to the contents of this <tt>StringBuffer</tt>, increasing the * length of this <tt>StringBuffer</tt> by the length of the argument. * If <tt>sb</tt> is <tt>null</tt>, then the four characters * <tt>"null"</tt> are appended to this <tt>StringBuffer</tt>. * <p> * Let <i>n</i> be the length of the old character sequence, the one * contained in the <tt>StringBuffer</tt> just prior to execution of the * <tt>append</tt> method. Then the character at index <i>k</i> in * the new character sequence is equal to the character at index <i>k</i> * in the old character sequence, if <i>k</i> is less than <i>n</i>; * otherwise, it is equal to the character at index <i>k-n</i> in the * argument <code>sb</code>. * <p> * This method synchronizes on <code>this</code> (the destination) * object but does not synchronize on the source (<code>sb</code>). * * @param sb the <tt>StringBuffer</tt> to append. * @return a reference to this object. * @since 1.4 */ public synchronized StringBuffer append(StringBuffer sb) { super.append(sb); return this;
StringBuffer/StringBuilder是生产这个商品的流水线,
StringBuffer速度慢,但(线程)安全性高
StringBuilder速度快,但(线程)安全性差
String为不可变得对象意味着每次都需要创建对象来进行重新复制
StringBuffer则是可变的。效率上要高很多,因为不会产生垃圾对象
StringBuffer和StringBuilder可变,对其修改不会产生新的对象 其两者区别在于StringBuffer线程安全而StringBuilder线程不安全
StringBuffer 是用于频繁修改的,而且是线程安全;
StringBuilder 我不怎么用,所以不了解,而且是线程不安全。如果只是用来声明一个变量的话就用String,如果用于字符串之间的频繁操作的话就用StringBuffer.
StringBuffer/StringBuilder是生产这个商品的流水线,
StringBuffer速度慢,但(线程)安全性高
StringBuilder速度快,但(线程)安全性差
很恰当的。。
StringBuffer 线程安全的可变字符序列
StringBuilder 非线程安全的可变字符序列
最普遍的误区之一:“StringBuffer/Vector/Hashtable 是线程安全的”“线程安全”是很复杂的事情,没有“通用”的解决方案,需要“因地制宜”,不要真的以为用了上述的类就真的“线程安全”了。事实是,StringBuffer/Vector/Hashtable 由于设计缺陷,几乎没什么用,事实上早就被废弃了。
PS:你可以查看java API文档啊 上面有很详细的介绍
StringBuffer 提供了数值可变的字符串,且线程安全
StringBuilder 提供了数值可变的字符串,但线程不安全
String 在常量池中或堆中
StringBuffer 是线程安全,append 方法被定义为synchronized 效率较低
/**
* Appends the specified <tt>StringBuffer</tt> to this sequence.
* <p>
* The characters of the <tt>StringBuffer</tt> argument are appended,
* in order, to the contents of this <tt>StringBuffer</tt>, increasing the
* length of this <tt>StringBuffer</tt> by the length of the argument.
* If <tt>sb</tt> is <tt>null</tt>, then the four characters
* <tt>"null"</tt> are appended to this <tt>StringBuffer</tt>.
* <p>
* Let <i>n</i> be the length of the old character sequence, the one
* contained in the <tt>StringBuffer</tt> just prior to execution of the
* <tt>append</tt> method. Then the character at index <i>k</i> in
* the new character sequence is equal to the character at index <i>k</i>
* in the old character sequence, if <i>k</i> is less than <i>n</i>;
* otherwise, it is equal to the character at index <i>k-n</i> in the
* argument <code>sb</code>.
* <p>
* This method synchronizes on <code>this</code> (the destination)
* object but does not synchronize on the source (<code>sb</code>).
*
* @param sb the <tt>StringBuffer</tt> to append.
* @return a reference to this object.
* @since 1.4
*/
public synchronized StringBuffer append(StringBuffer sb) {
super.append(sb);
return this;
StringBuilder与StringBuffer 实现方式类似 非线程安全 效率高
public StringBuilder append(String str) {
super.append(str);
return this;
}StringBuffer 、StringBuilder父类都是AbstractStringBuilder
简单来说,String长度不可变,StringBuffer和StringBuilder都是长度可变的。
同时,StringBuffer是线程安全的,而StringBuilder是非线程安全的,
所以,StringBuilder的效率要比StringBuffer高。但是String的效率是最低的,
我做过实验,StringBuilder和StringBuffer的效率比String高100多倍,所以
区别很显然,如果想详细了解它们的运行机制,介意楼主去看源码吧。
楼主难道是最近面试被问到了吗?
String越追加越慢,完成需要大概1分钟,StringBuffer仅需1秒。
因为String每次追加需要创建对象。