String, StringBuffer, StringBuilder区别

  • String是非可变类,其对象为字符串常量,不适合频繁改变,如插入(insert),删除(delete),添加(append)等。
  • StringBuffer是可变类,其对象为可修改的字符序列,比较适合用来频繁的修改字符串,比如常用的是添加(append);而且是线程安全的。当需要多次修改时,尽量用这个类。
具体地说,当你使用String b = b+"aa"; 这样的语句的时候 b 实际上已经不是以前的那个对象, JVM 重新划分了一块内存保存 b+"aa" 的结果,并使 b 的指向这块新内存。对于 StringBuffer 来说, b.append("aa"); 执行以后 b 仍然使用的是以前的那块内存空间。同时,对于原来b指向的内存还有内存回收的问题。
  • StringBuilder是可变类,但基于单线程的,也就是说多线程是不安全的,其余和StringBuffer一样。所以速度相对较快,如果不涉及多线程,建议使用这个。
public final class String
extends Object implements Serializable , Comparable < String >, CharSequence

TheString class represents character strings. All string literals in Java programs, such as"abc" , are implemented as instances of this class.

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. For example:

     String str = "abc";

is equivalent to:

     char data[] = {'a', 'b', 'c'};
String str = new String(data);

Here are some more examples of how strings can be used:

     System.out.println("abc");
String cde = "cde";
System.out.println("abc" + cde);
String c = "abc".substring(2,3);
String d = cde.substring(1, 2);

The classString includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by theCharacter class.

The Java language provides special support for the string concatenation operator (+), and for conversion of other objects to strings.String concatenation is implemented through theStringBuilder (orStringBuffer ) class and itsappend method. String conversions are implemented through the methodtoString , defined byObject and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele,The Java Language Specification .

Unless otherwise noted, passing anull argument to a constructor or method in this class will cause aNullPointerException to be thrown.

AString represents a string in the UTF-16 format in whichsupplementary characters are represented bysurrogate pairs (see the sectionUnicode Character Representations in theCharacter class for more information). Index values refer tochar code units, so a supplementary character uses two positions in aString .

TheString class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e.,char values).

Since:
JDK1.0

public final class StringBuffer
extends Object implements Serializable , CharSequence

A thread-safe, mutable sequence of characters. A string buffer is like aString , 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 aStringBuffer are theappend andinsert 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. Theappend method always adds these characters at the end of the buffer; theinsert method adds the characters at a specified point.

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

In general, if sb refers to an instance of aStringBuffer , thensb.append(x) has the same effect assb.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 .TheStringBuilder 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.

Since:
JDK1.0

public final class StringBuilder
extends Object implements Serializable , CharSequence

A mutable sequence of characters. This class provides an API compatible withStringBuffer , but with no guarantee of synchronization.This class is designed for use as a drop-in replacement forStringBuffer 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 toStringBuffer as it will be faster under most implementations. (faster!)

The principal operations on aStringBuilder are theappend andinsert 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. Theappend method always adds these characters at the end of the builder; theinsert method adds the characters at a specified point.

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

In general, if sb refers to an instance of aStringBuilder , thensb.append(x) has the same effect assb.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 ofStringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended thatStringBuffer be used.

Since:
1.5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值