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.

String, StringBuilder 与StringBuffer的区别与联系相关推荐

  1. String、StringBuilder和StringBuffer的区别和用法

    分别使用使用这三种来拼接字符串,对比各自损耗的时间: 经过测试: package com.test;public class Main{public static void main(String[] ...

  2. Java计基础---常用类之StringBuilder类--String、StringBuilder和StringBuffer 的区别

    常用类之StringBuilder类 StringBulider 类:也是一个字符串类.是一个可变的字符序列,在类中提供了可以改变字符串内容的常用的方法. StringBulider 和 String ...

  3. Java String,StringBuilder和StringBuffer的区别 StringBuilder StringBuffer String

    可以证明,字符串操作是计算机程序设计中最常见的行为. String:不可变的对象,对String对象进行改变的时候其实都等同于生成了一个新的String对象,然后将引用指向新的String对象,原St ...

  4. String、StringBuilder、StringBuffer的区别

    它们之间的区别: 在我们学习String类的时候,也会学习到StringBuilder和StringBuffer,但是他们之间有什么区别呢? 当然他们在具体的代码实现上.内存分配上以及效率上都有着不同 ...

  5. Java中的String,StringBuilder,StringBuffer的区别

    这三个类之间的区别主要是在两个方面,即运行速度和线程安全这两方面. 首先说运行速度,或者说是执行速度,在这方面运行速度快慢为:StringBuilder > StringBuffer > ...

  6. Java 中的 String、StringBuilder、StringBuffer 的区别

    目录 一.是什么? 二.区别是? 1. 运行速度(执行速度) 2. 线程安全 三.小结 四.加餐 一.是什么? String 不可变字符序列 String 是字符串常量,其对象一旦创建之后该对象是不可 ...

  7. StringBuilder与StringBuffer的区别(转)

    相信大家看到过很多比较String和StringBuffer区别的文章,也明白这两者的区别,然而自从Java 5.0发布以后,我们的比较列表上将多出一个对象了,这就是StringBuilder类.St ...

  8. 「每天一道面试题」String和StringBuilder、StringBuffer的区别

    Java提供了两种类型的字符串:不可变字符串和可变字符串,分别是String和StringBuffer/StringBuilder,其中String引用的字符串内容不能被改变,而StringBuffe ...

  9. String和StringBuilder、StringBuffer的区别?

    Java平台提供了两种类型的字符串:String和StringBuffer/StringBuilder,它们可以储存和操作字符串.其中String是只读字符串,也就意味着String引用的字符串内容是 ...

最新文章

  1. centos 日志切割_centos 创建 logrotate 进行日志分割
  2. 【pmcaff专栏】吴波:浅谈O2O行业的猎人与农夫
  3. 一条mysql分组查询的问题分析
  4. cocos2d-x游戏开发(十四)用shader使图片背景透明
  5. 机器人穿法_(图解)机器人系统组成介绍
  6. win10 uwp 改变鼠标
  7. pr调色预设_视频调色不好掌握?用这2000套PR、AE、达芬奇调色预设吧
  8. vSphere 7 With K8s系列-1~9 (微信公众号需要收费)
  9. k8s核心技术-Controller(statefulSet)_部署有状态应用---K8S_Google工作笔记0033
  10. Spark与MR的区别
  11. 小猿圈分享Javascript技巧(下)
  12. python实现调用百度图像识别api得到图片识别与检测类别和详细信息以及相关准确度
  13. 镀镍金刚石线切割硅片
  14. I2C总线与EPPROM
  15. Linux系列: 777 755区别
  16. 快手科技更新招股书:前11个月营收525亿元,引入10家基石投资者
  17. python-urllib3
  18. 就业信息管理系统的设计与实现
  19. C5t:递归判断是否回文串
  20. php习题,PHP程序设计试题与答案

热门文章

  1. 进程P1、P2、P3、P4和P5的前趋图如下图所示。若用PV操作控制进程P1~P5并发执行的过程,则需要设置6个信号S1、S2、S3、S4,且信号量S1~S4的初值都等于0。下图中a和b处应分别填写(
  2. ironpython使用dictionary_在C#环境中动态调用IronPython脚本(一)
  3. 理论+实验·MHA高可用配置及故障切换
  4. 字典统计排序python123_按值对字典进行排序Python(3级Dict)
  5. 安宁计算机学院,合肥工业大学计算机与信息学院导师介绍:安宁
  6. 计算机控制的工频机是什么,UPS 如何分类,工频机和高频机区别是什么?
  7. 51单片机auxr寄存器_STC12C5A60S2单片机AD采样程序及其寄存器讲解
  8. vueform表单文件上传_峰哥说技术系列-8.Spring Boot文件上传(Form表单和Ajax方式)
  9. 表单提交时submit验证非空return false没用_开发这样一个复杂的表单你需要用多久...
  10. .计算机自动关机或重启,电脑自动关机或者重启怎么处理