java.lang(StringBuffer)
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence {
/**
* Constructs a string buffer with no characters in it and an
* initial capacity of 16 characters.
*/
public StringBuffer() {
super(16);
}
/**
* Constructs a string buffer with no characters in it and
* the specified initial capacity.
*/
public StringBuffer(int capacity) {
super(capacity);
}
/**
* Constructs a string buffer initialized to the contents of the
* specified string. The initial capacity of the string buffer is
* <code>16</code> plus the length of the string argument.
*
* @param str the initial contents of the buffer.
* @exception NullPointerException if <code>str</code> is <code>null</code>
*/
public StringBuffer(String str) {
super(str.length() + 16);
append(str);
}

StringBuffer 是一个线程安全的可变的字符序列。它继承于AbstractStringBuilder,实现了CharSequence接口。
StringBuilder 也是继承于AbstractStringBuilder的子类;但是,StringBuilder和StringBuffer不同,前者是非线程安全的,后者是线程安全的。
StringBuffer的内部实现方式和String不同,StringBuffer在进行字符串处理时,不生成新的对象,在内存使用上要优于String类。
所以在实际使用时,如果经常需要对一个字符串进行修改,例如插入、删除等操作,使用StringBuffer要更加适合一些。
在StringBuffer类中存在很多和String类一样的方法,这些方法在功能上和String类中的功能是完全一样的。
但是有一个最显著的区别在于,对于StringBuffer对象的每次修改都会改变对象自身,这点是和String类最大的区别。
对于StringBuffer的append操作基本是在AbstractStringBuilder继承实现,看源码:
abstract class AbstractStringBuilder implements Appendable, CharSequence {
char[] value; //The value is used for character storage.
int count; //The count is the number of characters used.
//Appends the specified string to this character sequence.
public AbstractStringBuilder append(String str) {
if (str == null) str = "null";
int len = str.length();
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
// Documentation in subclasses because of synchro difference
public AbstractStringBuilder append(StringBuffer sb) {
if (sb == null)
return append("null");
int len = sb.length();
ensureCapacityInternal(count + len);
sb.getChars(0, len, value, count);
count += len;
return this;
}
// Documentation in subclasses because of synchro difference
public AbstractStringBuilder append(CharSequence s) {
if (s == null)
s = "null";
if (s instanceof String)
return this.append((String)s);
if (s instanceof StringBuffer)
return this.append((StringBuffer)s);
return this.append(s, 0, s.length());
}
/**
* Appends the string representation of the {@code char} array
* argument to this sequence.
*/
public AbstractStringBuilder append(char[] str) {
int len = str.length;
ensureCapacityInternal(count + len);
System.arraycopy(str, 0, value, count, len);
count += len;
return this;
}
}
/***************其他追加字符方法省略介绍了***********/
添加字符时的扩容方法介绍:
/**
* This implements the expansion semantics of ensureCapacity with no
* size check or synchronization.
*/
void expandCapacity(int minimumCapacity) {
int newCapacity = value.length * 2 + 2;
if (newCapacity - minimumCapacity < 0)
newCapacity = minimumCapacity;
if (newCapacity < 0) {
if (minimumCapacity < 0) // overflow
throw new OutOfMemoryError();
newCapacity = Integer.MAX_VALUE;
}
value = Arrays.copyOf(value, newCapacity);
}
每次添加字符时扩容为:int newCapacity = value.length * 2 + 2;
java.lang(StringBuffer)的更多相关文章
- Java源码学习 -- java.lang.StringBuilder,java.lang.StringBuffer,java.lang.AbstractStringBuilder
一直以来,都是看到网上说“ StringBuilder是线程不安全的,但运行效率高:StringBuffer 是线程安全的,但运行效率低”,然后默默记住:一个是线程安全.一个线程不安全,但对内在原因并 ...
- java.lang.StringBuffer源码分析
StringBuffer是一个线程安全的可变序列的字符数组对象,它与StringBuilder一样,继承父类AbstractStringBuilder.在多线程环境中,当方法操作是必须被同步,Stri ...
- < java.lang >-- StringBuffer字符串缓冲区
构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符. 特点: 1:可以对字符串内容进行修改. 2:是一个容器. 3:是可变长度的. 4:缓冲区中可以存储任意类型的数据. 5:最终需要变成字符 ...
- java.lang.StringBuilder和java.lang.StringBuffer (JDK1.8)
这两个类都是继承自AbstractStringBuilder,AbstractStringBuilder有两个成员属性 char[] value; int count; 前者用于存储字符串,后者用于统 ...
- JAVA String,StringBuffer与StringBuilder的区别??
String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要性能 ...
- Java String StringBuffer StringBuilder
String 字符串常量存储在常量区,每次追加操作会创建新的对象: StringBuffer 字符串变量 线程安全 在堆上创建,每次追加操作在原对象上进行操作: 速度 StringBuffer ...
- Java源码学习 -- java.lang.String
java.lang.String是使用频率非常高的类.要想更好的使用java.lang.String类,了解其源代码实现是非常有必要的.由java.lang.String,自然联想到java.lang ...
- Java之StringBuffer,StringBuilder,Math,Date,SimpleDateFormat,UUID,File
java.lang 类 StringBuffer java.lang.Object java.lang.StringBuffer 所有已实现的接口: Serializable, Appendable, ...
- 排查sqoop报错:Error running child : java.lang.OutOfMemoryError: Java heap space
报错栈: -- ::, INFO [main] org.apache.hadoop.mapred.MapTask: Processing split: = AND = -- ::, INFO [mai ...
随机推荐
- vue指令详解和自定义指令
在vue中,指令以v-开头,是一种特殊的自定义行间属性,指令的职责就是其表达式的值改变时相应地将某些行为应用到DOM上 指令使用的示例 在下面的运行结果中可以看到,v-html是可以解析html标签的 ...
- selenium 使用
selenium selenium:可以让浏览器完成相关自动化的操作 环境安装: pip install selenium 编码流程: 导包 创建某一款浏览器对象 制定相关的行为动作 from sel ...
- linux crontab定时器
1.查看linux是否有crontab指令 如果没有安装crontab指令 yum install -y vixie-cron yum -y install crontabs 2.设置开机自启 ch ...
- Java 8 新特性:4-断言(Predicate)接口
(原) 这个接口主要用于判断,先看看它的实现,说明,再给个例子. /* * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All ri ...
- UVA120-Stacks of Flapjacks(思维)
Problem UVA120-Stacks of Flapjacks Accept: 9232 Submit: 38455Time Limit: 10000 mSec Problem Descrip ...
- [HEOI2016/TJOI2016]排序
嘟嘟嘟 首先这题的暴力是十分好写的,而且据说能得不少分. 正解写起来不难,就是不太好想. 根据做题经验,我想到了给这个序列转化成01序列,但是接下来我就不会了.还是看了题解. 因为查询只有一个数,所以 ...
- ClickHouse最简单的安装方法
安装包地址: https://packagecloud.io/Altinity/clickhouse 无需下载安装包,更新yum源即可!! 最后: yum install -y clickhouse- ...
- range()函数
range()函数 函数说明: range(start, stop[, step]) -> range object,根据start与stop指定的范围以及step设定的步长,生成一个序列.参数 ...
- 5239-回忆京都-洛谷3月赛gg祭
传送门 题目背景 第十五届东方人气投票 音乐部门 106名 第四次国内不知道东方的人对东方原曲的投票调查 51名 回忆京都副歌我tm吹爆,东方文花帖我tm吹爆! 题目描述 射命丸文在取材中发现了一个好 ...
- argparse模块
argparse模块是的编写用户友好的命令行接口非常容易.程序只需定义好它要求的参数,然后argparse将负责如何从sys.argv中解析出这些参数.argparse模块还会自动生成帮助和使用信息并 ...