String, StringBuilder 与StringBuffer的区别与联系

1、区别

(1)String构建的对象不能改变,每次对String进行操作时,如两个String相加,需要新建一个String对象,然后容纳最终的结果。

而StringBuilder与StringBuffer构建的对象可以随时在修改其内容,而无需生成新的对象。一般新建一个对象是会生成16个字节的空间,之后根据需要再增加空间。

由于一般新构建一个对象涉及分配内存空间分配、无引用对象过多时的垃圾回收等,因此,对于操作频繁的字符串需使用StringBuilder或StringBuffer。

(2)StringBuilder与StringBuffer二者之间的区别在于前者的性能更高,但非线程安全的。而StringBuffer是线程安全的。

因此在大部分情况下,建议使用StringBuffer,除非涉及多线程情况。

2、官方文档中的一些说明

(1)String

The String class represents character strings. Strings are constant; their values cannot be changed after
they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.

(2)StringBuilder

A mutable sequence of characters.
This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. 因此,StringBuilder与StringBuffer的 API基本完全一致。

This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread
(as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept
data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds
these characters at the end of the builder; the insertmethod adds the characters at a specified point.

For example, if z refers to a string builder object whose current contents are "start", then the method call z.append("le") would
cause the string builder to contain "startle", whereas z.insert(4, "le") would alter the string builder to contain "starlet".

In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x).
Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer
overflows, it is automatically made larger.

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be
used.

(3)StringBuffer

A thread-safe, mutable sequence of characters. A string buffer is like a String,
but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

String buffers are safe for use by multiple threads.
The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the
individual threads involved.

The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept
data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds
these characters at the end of the buffer; the insert method adds the characters at a specified point.

For example, if z refers to a string buffer object whose current contents are "start", then the method call z.append("le") would
cause the string buffer to contain "startle", whereas z.insert(4, "le") would alter the string buffer to contain "starlet".

In general, if sb refers to an instance of a StringBuffer, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence) this class synchronizes only on the string buffer performing the operation, not on the source.

Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer
overflows, it is automatically made larger. As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder.
The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

String, StringBuilder 与StringBuffer的区别与联系

时间: 2024-10-13 19:24:58

String, StringBuilder 与StringBuffer的区别与联系的相关文章

String, StringBuilder及StringBuffer的区别

在java中,String, StringBuilder及StringBuffer经常被用来处理字符串操作. 下表列出它们的异同点: String StringBuffer StringBuilder 是否可继承 否(final) 否(final) 否(final) 是否长度可变 否 是 是 是否线程安全 ---------- 是 否 拼接效率 低 中 高 一般情况下,使用String,StringBuilder比较多.因为字符串的拼接很少会涉及到多线程,所以StringBuffer用的较少.先

String和StringBuilder、StringBuffer的区别

String和StringBuilder.StringBuffer的区别?     Java平台提供了两种类型的字符串:String和StringBuffer/StringBuilder,它们可以储存和操作字符串.其中String是只读字符串,也就意味着String引用的字符串内容是不能被改变的.而StringBuffer/StringBuilder类表示的字符串对象可以直接进行修改.StringBuilder是Java 5中引入的,它和StringBuffer的方法完全相同,区别在于它是在单线

String、StringBuilder和StringBuffer的区别和用法

分别使用使用这三种来拼接字符串,对比各自损耗的时间: 经过测试: package com.test; public class Main{ public static void main(String[] args){ testString(); testStringBuffer(); testStringBuilder(); } private static void testStringBuilder() { long begin = System.currentTimeMillis();

CharSequence,String ,Stringbuilder和StringBuffer源码分析

1.从类的定义看CharSequence.StringBuffer.StringBuilder.String的关系 下面先贴上这四者的定义(来自JDK1.6) CharSequence是一个定义字符串操作的接口,StringBuffer.StringBuilder.String中都实现了这个接口. //CharSequence定义 public interface CharSequence //StringBuffer定义 public final class StringBuffer exte

Java String,StringBuilder和StringBuffer的区别 StringBuilder > StringBuffer> String

可以证明,字符串操作是计算机程序设计中最常见的行为. String:不可变的对象,对String对象进行改变的时候其实都等同于生成了一个新的String对象,然后将引用指向新的String对象,原String对象GC回收.StringBuffer 字符串变量(线程安全),适用于多线程程序中,保证同步性.StringBuilder 字符串变量(非线程安全),适用于单线程程序中,不保证同步性.简要的说, String 类和 StringBuffer/StringBuilder 类的主要性能区别其实在

Java 中的String、StringBuilder与StringBuffer的区别联系(转载)

1 String 基础 想要了解一个类,最好的办法就是看这个类的源代码,String类源代码如下: public final class String implements java.io.Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ private final char value[]; /** Cache the hash code for t

Java中String、StringBuilder和StringBuffer的区别

---恢复内容开始--- String字符串常量 StringBuffer字符串变量(线程安全) StringBuilder字符串变量(非线程安全) String是常量,该对象的值已经被赋予了,不能再更改.若非要更改,则只能再重新创建另一个对象,在这个新创建的对象上再进行赋值. StringBuffer是变量,是可以随时变化的量.如果对StringBuffer对象进行改变,每次结果都会对StringBuffer对象进行操作,而不是生成新的对象.所以一般字符串要经常变化的话推荐使用StringBu

String,StringBuilder,StringBuffer的区别

1.速度 运行速度,或者说是执行速度,在这方面运行速度快慢为:StringBuilder > StringBuffer > String String最慢的原因:String为字符串常量,而StringBuilder和StringBuffer均为字符串变量,即String对象一旦创建之后该对象是不可更改的,但后两者的对象是变量,是可以更改的. 以下面一段代码为例:String str="abc"; System.out.println(str); str=str+"

StringBuilder与StringBuffer的区别(转)

相信大家看到过很多比较String和StringBuffer区别的文章,也明白这两者的区别,然而自从Java 5.0发布以后,我们的比较列表上将多出一个对象了,这就是StringBuilder类.String类是不可变类,任何对String的改变都会引发新的String对象的生成:而StringBuffer则是可变类,任何对它所指代的字符串的改变都不会产生新的对象,可变和不可变类这一对对象已经齐全了,那么为什么还要引入新的StringBuilder类干吗?相信大家都有此疑问,我也如此.下面,我们