String, StringBuilder 与StringBuffer的区别与联系

原创 2017年08月09日 11:52:06

String, StringBuilder 与StringBuffer的区别与联系

@(JAVA)[java]

1、区别

(1)String构建的对象不能改变,每次对String进行操作时,如两个String相加,需要新建一个String对象,然后容纳最终的结果。
而StringBuilder与StringBuffer构建的对象可以随时在修改其内容,而无需生成新的对象。一般新建一个对象是会生成16个字节的空间,之后根据需要再增加空间。
由于一般新构建一个对象涉及分配内存空间分配、无引用对象过多时的垃圾回收等,因此,对于操作频繁的字符串需使用StringBuilder或StringBuffer。

(2)StringBuilder与StringBuffer二者之间的区别在于前者的性能更高,但非线程安全的。而StringBuffer是线程安全的。
因此在大部分情况下,建议使用StringBuilder,除非涉及多线程情况。

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.

版权声明:本文为博主原创文章,转载请注明来自http://blog.csdn.net/jediael_lu/

StringBuffer 与 StringBuilder 区别与联系及源码分析

StringBuffer和StringBuilder的共同点: 1、都是用于操作字符串,使用这两个而不使用String的原因是因为String是Final类型,当对字符串操作较多时采用Str...
  • u010854517
  • u010854517
  • 2017年03月08日 10:07
  • 229

Java基础之String、StringBuffer与StringBuilder的区别及应用场景

1 String、StringBuffer与StringBuilder (1)String 字符串常量; /** Strings are constant; their values cann...
  • chenliguan
  • chenliguan
  • 2016年07月14日 18:35
  • 6249

StringBuilder的用法及String,StringBuffer与StringBuilder的区别

声明:本文大部分内容为参考别人的,自己简单汇总整理了一下。String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全)简要的说,...
  • u011489043
  • u011489043
  • 2017年03月30日 15:33
  • 541

面试题之:String,StringBuffer与StringBuilder的区别?

String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全)  简要的说, String 类型和 StringBuffer 类...
  • u014381710
  • u014381710
  • 2015年08月24日 23:23
  • 429

String,StringBuffer,StringBuilder之间的区别和联系

tring是字符串常量。StringBuffer是字符串变量 ,线程安全。StringBuilder是字符串变量,线程不安全。 速度:String 为什么String速度最慢呢? ...
  • xuwanlong111
  • xuwanlong111
  • 2014年12月29日 16:25
  • 448

Java面试之谈谈String,StringBuilder,StringBuffer区别

引言 这是一个很常见的问题了,在这里我并不是要提出什么不一样的观点,只是想通过分析源码来加深对这一问题的理解。如果您恰好也感兴趣的话,不妨随我一读。 注意:以下源码来自orcal JDK1.8。 ...
  • jinwei__
  • jinwei__
  • 2016年11月03日 16:17
  • 414

String、StringBuffer、StringBuilder的区别与效率比较

String 是不可变的,StringBuffer、StringBuilder是可变的 String 、StringBuffer是线程安全的,StringBuilder是线程不安全的 (StringB...
  • u010002184
  • u010002184
  • 2017年05月18日 15:15
  • 418

String,StringBuffer,StringBuilder 三者之间的区别是什么?

1.String 字符串产量 2.StringBuffer 字符串变量(线程安全) 3.StringBuilder 字符串变量 (非线程安全) 1.String类型,是java内的对象,...
  • qq_30278639
  • qq_30278639
  • 2016年05月10日 14:09
  • 852

面试必备:String,StringBuffer,StringBuilder区别

首先说一下,这三个类都是用来处理字符串的, StringBuilder是JavaSE5之后引入的,用来替代StringBuffer, StringBuffer相对于StringBuilder线...
  • zrf1335348191
  • zrf1335348191
  • 2016年01月30日 10:21
  • 1007

StringBuffer与StringBuilder之间的区别

public class Test { public static void main(String[] args) { StringBuffer strBuffer = new StringB...
  • a19881029
  • a19881029
  • 2014年03月02日 13:25
  • 2589
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:String, StringBuilder 与StringBuffer的区别与联系
举报原因:
原因补充:

(最多只允许输入30个字)