JDK1.8源码学习--lang包(StringBuffer)
前言
月是一轮明镜,晶莹剔透,代表着一张白纸(啥也不懂)
央是一片海洋,海乃百川,代表着一块海绵(吸纳万物)
泽是一柄利剑,千锤百炼,代表着千百锤炼(输入输出)
月央泽,学习的一种过程,从白纸->吸收各种知识->不断输入输出变成自己的内容
希望大家一起坚持这个过程,也同样希望大家最终都能从零到零,把知识从薄变厚,再由厚变薄!
一.StringBuffer的作用:
直接看源码注释(我的翻译可能不太准,如果道友们有更棒的理解,可以留言或者私信)
/*** A thread-safe, mutable sequence of characters.* A string buffer is like a {@link 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.* 1.线程安全的、可变的字符序列。字符串缓冲区类似于String,但可以修改。* 在任何时候,它都包含一些特定的字符序列,但是可以通过某些方法调用来更改序列的长度和内容* <p>* 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.* 2.字符串缓冲区可供多个线程安全使用。这些方法在必要时被同步,以便任何特定实例上的所有操作都表现得好像它们以某种串行顺序发生,* 该顺序与所涉及的每个单独线程进行的方法调用的顺序一致* <p>* The principal operations on a {@code StringBuffer} are the* {@code append} and {@code 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* {@code append} method always adds these characters at the end* of the buffer; the {@code insert} method adds the characters at* a specified point.* 3.StringBuffer上的主要操作是append和insert方法,它们被重载以接受任何类型的数据。* 每个有效地将给定的数据转换为字符串,然后将该字符串的字符附加或插入到字符串缓冲区中。* append方法总是在缓冲区的末尾添加这些字符; insert方法在指定点添加字符。* <p>* For example, if {@code z} refers to a string buffer object* whose current contents are {@code "start"}, then* the method call {@code z.append("le")} would cause the string* buffer to contain {@code "startle"}, whereas* {@code z.insert(4, "le")} would alter the string buffer to* contain {@code "starlet"}.* 4.例如,如果z引用当前内容为"start"的字符串缓冲区对象,则方法调用z.append("le")* 将导致字符串缓冲区包含"startle",而 z.insert(4, "le")会将字符串缓冲区更改为包含 {@code "starlet"}。* <p>* In general, if sb refers to an instance of a {@code StringBuffer},* then {@code sb.append(x)} has the same effect as* {@code sb.insert(sb.length(), x)}.* 5.一般来说,如果 sb 引用了一个StringBuffer的实例,* 那么sb.append(x)和sb.insert(sb.length(), x)的效果是一样的。* <p>* 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.* Note that while {@code StringBuffer} is designed to be safe to use* concurrently from multiple threads, if the constructor or the* {@code append} or {@code insert} operation is passed a source sequence* that is shared across threads, the calling code must ensure* that the operation has a consistent and unchanging view of the source* sequence for the duration of the operation.* This could be satisfied by the caller holding a lock during the* operation's call, by using an immutable source sequence, or by not* sharing the source sequence across threads.* 6.每当发生涉及源序列的操作(例如从源序列追加或插入)时,此类仅在执行操作的字符串缓冲区上同步,* 而不是在源上同步。请注意,虽然StringBuffer被设计为可以安全地从多个线程并发使用,* 但如果构造函数或append或insert操作传递了一个跨线程共享的源序列,* 则调用代码必须确保操作在操作期间具有一致且不变的源序列视图。* 这可以通过调用者在操作调用期间持有锁、使用不可变源序列或不跨线程共享源序列来满足。* <p>* 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.* 7.每个字符串缓冲区都有一个容量。只要字符串缓冲区中包含的字符序列的长度不超过容量,* 就不需要分配新的内部缓冲区数组。如果内部缓冲区溢出,它会自动变大。* <p>* Unless otherwise noted, passing a {@code null} argument to a constructor* or method in this class will cause a {@link NullPointerException} to be* thrown.* 8.除非另有说明,否则将null参数传递给此类中的构造函数或方法将导致抛出NullPointerException* <p>* As of release JDK 5, this class has been supplemented with an equivalent* class designed for use by a single thread, {@link StringBuilder}. The* {@code 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.* 9从 JDK 5 版本开始,这个类已经补充了一个设计用于单线程的等效类,StringBuilder。* StringBuilder类通常应该优先于这个类使用,因为它支持所有相同的操作,但速度更快,因为它不执行同步** @author Arthur van Hoff* @see java.lang.StringBuilder* @see java.lang.String* @since JDK1.0*/
二.类图:
三.成员变量:
/*** StringBuffer 的可序列化字段。* @serialField value char[]* The backing character array of this StringBuffer.* 此 StringBuffer 的支持字符数组。* @serialField count int* The number of characters in this StringBuffer.* 此 StringBuffer 中的字符数。* @serialField shared boolean* A flag indicating whether the backing array is shared.* The value is ignored upon deserialization.* 指示后备数组是否共享的标志。反序列化时忽略该值。*/ private static final java.io.ObjectStreamField[] serialPersistentFields ={new java.io.ObjectStreamField("value", char[].class),new java.io.ObjectStreamField("count", Integer.TYPE),new java.io.ObjectStreamField("shared", Boolean.TYPE),};/*** toString 返回的最后一个值的缓存。每当修改 StringBuffer 时清除*/private transient char[] toStringCache;
四.构造方法:
/*** 构造一个没有字符的字符串缓冲区,初始容量为 16 个字符。*/public StringBuffer() {super(16);}/*** 构造一个字符串缓冲区,其中没有字符且具有指定的初始容量。*/public StringBuffer(int capacity) {super(capacity);}/*** 构造一个初始化为指定字符串内容的字符串缓冲区。字符串缓冲区的初始容量是16加上字符串参数的长度*/public StringBuffer(String str) {super(str.length() + 16);append(str);}/*** 1.构造一个字符串缓冲区,其中包含与指定的CharSequence相同的字符。* 字符串缓冲区的初始容量是16加上CharSequence参数的长度。* 2.如果指定的CharSequence的长度小于或等于 0,则返回容量为16的空缓冲区*/public StringBuffer(CharSequence seq) {this(seq.length() + 16);append(seq);}
五.内部方法:
length
@Overridepublic synchronized int length() {return count;}
capacity
@Overridepublic synchronized int capacity() {return value.length;}@Overridepublic synchronized void ensureCapacity(int minimumCapacity) {super.ensureCapacity(minimumCapacity);}
trimToSize
@Overridepublic synchronized void trimToSize() {super.trimToSize();}
setLength
@Overridepublic synchronized void setLength(int newLength) {toStringCache = null;super.setLength(newLength);}
charAt
public synchronized char charAt(int index) {if ((index < 0) || (index >= count))throw new StringIndexOutOfBoundsException(index);return value[index];}
codePoint
@Overridepublic synchronized int codePointBefore(int index) {return super.codePointBefore(index);}@Overridepublic synchronized int codePointCount(int beginIndex, int endIndex) {return super.codePointCount(beginIndex, endIndex);}@Overridepublic synchronized int offsetByCodePoints(int index, int codePointOffset) {return super.offsetByCodePoints(index, codePointOffset);}
getChars
public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,int dstBegin){super.getChars(srcBegin, srcEnd, dst, dstBegin);}
setCharAt
public synchronized void setCharAt(int index, char ch) {if ((index < 0) || (index >= count))throw new StringIndexOutOfBoundsException(index);toStringCache = null;value[index] = ch;}
append
@Overridepublic synchronized StringBuffer append(Object obj) {toStringCache = null;super.append(String.valueOf(obj));return this;}@Overridepublic synchronized StringBuffer append(String str) {toStringCache = null;super.append(str);return this;}/*** 1. 将指定的StringBuffer附加到此序列。* 2.StringBuffer参数的字符按顺序附加到此StringBuffer的内容中,将此StringBuffer的长度增加参数的长度。* 如果 sb为 null,则将四个字符 "null"附加到此StringBuffer。* 3.设n是旧字符序列的长度,即在append方法执行之前包含在StringBuffer中的字符序列。* 那么新字符序列中索引k处的字符等于旧字符序列中索引k处的字符,如果k小于n;否则,* 它等于参数 sb中索引k-n处的字符。*4.此方法在目标对象this上同步,但不会在源 sb上同步*/public synchronized StringBuffer append(StringBuffer sb) {toStringCache = null;super.append(sb);return this;}synchronized StringBuffer append(AbstractStringBuilder asb) {toStringCache = null;super.append(asb);return this;}/*** 1.将指定的 CharSequence附加到此序列。* 2.CharSequence参数的字符按顺序附加,通过参数的长度增加此序列的长度。* 3.此方法的结果与调用 this.append(s, 0, s.length()); 完全相同* 4.此方法在目标对象this上同步,但不会在源 (s) 上同步。*5.如果 s为 null,则附加四个字符 "null"。*/@Overridepublic synchronized StringBuffer append(CharSequence s) {toStringCache = null;super.append(s);return this;}@Overridepublic synchronized StringBuffer append(CharSequence s, int start, int end){toStringCache = null;super.append(s, start, end);return this;}@Overridepublic synchronized StringBuffer append(char[] str) {toStringCache = null;super.append(str);return this;}@Overridepublic synchronized StringBuffer append(char[] str, int offset, int len) {toStringCache = null;super.append(str, offset, len);return this;}@Overridepublic synchronized StringBuffer append(boolean b) {toStringCache = null;super.append(b);return this;}@Overridepublic synchronized StringBuffer append(char c) {toStringCache = null;super.append(c);return this;}@Overridepublic synchronized StringBuffer append(int i) {toStringCache = null;super.append(i);return this;}/*** @since 1.5*/@Overridepublic synchronized StringBuffer appendCodePoint(int codePoint) {toStringCache = null;super.appendCodePoint(codePoint);return this;}@Overridepublic synchronized StringBuffer append(long lng) {toStringCache = null;super.append(lng);return this;}@Overridepublic synchronized StringBuffer append(float f) {toStringCache = null;super.append(f);return this;}@Overridepublic synchronized StringBuffer append(double d) {toStringCache = null;super.append(d);return this;}
delete
@Overridepublic synchronized StringBuffer delete(int start, int end) {toStringCache = null;super.delete(start, end);return this;}@Overridepublic synchronized StringBuffer deleteCharAt(int index) {toStringCache = null;super.deleteCharAt(index);return this;}
replace
public synchronized StringBuffer replace(int start, int end, String str) {toStringCache = null;super.replace(start, end, str);return this;}
subString
@Overridepublic synchronized String substring(int start) {return substring(start, count);}@Overridepublic synchronized CharSequence subSequence(int start, int end) {return super.substring(start, end);}@Overridepublic synchronized String substring(int start, int end) {return super.substring(start, end);}
insert
@Overridepublic synchronized StringBuffer insert(int index, char[] str, int offset,int len){toStringCache = null;super.insert(index, str, offset, len);return this;}public synchronized StringBuffer insert(int offset, Object obj) {toStringCache = null;super.insert(offset, String.valueOf(obj));return this;}public synchronized StringBuffer insert(int offset, String str) {toStringCache = null;super.insert(offset, str);return this;}public synchronized StringBuffer insert(int offset, char[] str) {toStringCache = null;super.insert(offset, str);return this;}public StringBuffer insert(int dstOffset, CharSequence s) {//注意,在将 s 缩小到特定类型 Ditto 以清除 toStringCache 之后,通过调用其他 StringBuffer 方法实现同步super.insert(dstOffset, s);return this;}public synchronized StringBuffer insert(int dstOffset, CharSequence s,int start, int end){toStringCache = null;super.insert(dstOffset, s, start, end);return this;}public StringBuffer insert(int offset, boolean b) {//注意,通过超类方法 Ditto 将 b 转换为 String 后调用 StringBuffer insert(int, String) 实现同步以清除 toStringCachesuper.insert(offset, b);return this;}public synchronized StringBuffer insert(int offset, char c) {toStringCache = null;super.insert(offset, c);return this;}public StringBuffer insert(int offset, int i) {super.insert(offset, i);return this;}public StringBuffer insert(int offset, long l) {super.insert(offset, l);return this;}public StringBuffer insert(int offset, float f) {super.insert(offset, f);return this;}public StringBuffer insert(int offset, double d) {super.insert(offset, d);return this;}
indexOf
public int indexOf(String str) {// Note, synchronization achieved via invocations of other StringBuffer methodsreturn super.indexOf(str);}public synchronized int indexOf(String str, int fromIndex) {return super.indexOf(str, fromIndex);}public int lastIndexOf(String str) {// Note, synchronization achieved via invocations of other StringBuffer methodsreturn lastIndexOf(str, count);}public synchronized int lastIndexOf(String str, int fromIndex) {return super.lastIndexOf(str, fromIndex);}
reverse
public synchronized StringBuffer reverse() {toStringCache = null;super.reverse();return this;}
toString
public synchronized String toString() {if (toStringCache == null) {toStringCache = Arrays.copyOfRange(value, 0, count);}return new String(toStringCache, true);}
writeObject/readObject
/*** 调用 readObject 以从流中恢复 StringBuffer 的状态。*/private synchronized void writeObject(java.io.ObjectOutputStream s)throws java.io.IOException {java.io.ObjectOutputStream.PutField fields = s.putFields();fields.put("value", value);fields.put("count", count);fields.put("shared", false);s.writeFields();}/*** 调用 readObject 以从流中恢复 StringBuffer 的状态*/private void readObject(java.io.ObjectInputStream s)throws java.io.IOException, ClassNotFoundException {java.io.ObjectInputStream.GetField fields = s.readFields();value = (char[])fields.get("value", null);count = fields.get("count", 0);}
六.总结:
内部重要方法全是调用的父类.....
JDK1.8源码学习--lang包(StringBuffer)相关推荐
- JDK1.1源码学习之官方文档与代码结构
浩哥带你学习JDK1.1源码--第2天 1. 阅读源码那点事 2. JDK 1.1.8源码结构 1. 阅读源码那点事 在上一篇的文章中,进入Java官网的档案袋里面,可以看到文档有两个下载链接:一个日 ...
- Java多线程之JUC包:Semaphore源码学习笔记
若有不正之处请多多谅解,并欢迎批评指正. 请尊重作者劳动成果,转载请标明原文链接: http://www.cnblogs.com/go2sea/p/5625536.html Semaphore是JUC ...
- JAVA JDK 源码学习
JAVA JDK 源码学习 ,以1.8为例,按照下面图片顺序依次学习: applet ,awt,beans,io,lang,math,net,nio,rmi,security,sql,text,tim ...
- Android源码学习之浅析SystemServer脉络
在之前的博文中<Android源码学习之如何创建使用JNI>和<Android源码学习之如何使用eclipse+NDK>中,浅谈了如何创建使用JNI和如何利用NDK工具开发创建 ...
- clickhouse-jdbc 源码学习
clickhouse-jdbc 源码学习 文章目录 clickhouse-jdbc 源码学习 包介绍 依赖版本 搭建环境版本如下 QA 1.LocalDate/LocalDateTime不兼容 2.一 ...
- Apache log4j-1.2.17源码学习笔记
(1)Apache log4j-1.2.17源码学习笔记 http://blog.csdn.net/zilong_zilong/article/details/78715500 (2)Apache l ...
- android源码学习-Toast实现原理讲解
前言: 前些日志QQ群有朋友发了一个Toast的崩溃日志.Toast如此简单的用法怎么会崩溃呢?所以顺便就学习了一下Toast在源码中的实现,不算复杂,但内容挺多的,这里就来分享一下,方便读者. 一. ...
- JDK源码学习笔记——Integer
一.类定义 public final class Integer extends Number implements Comparable<Integer> 二.属性 private fi ...
- Java 源码学习系列(三)——Integer
Integer 类在对象中包装了一个基本类型 int 的值.Integer 类型的对象包含一个 int 类型的字段. 此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还 ...
最新文章
- Redis源码分析:服务器端处理过程
- css布局中的百分比布局
- [云炬创业基础笔记]第五章创业机会评估测试8
- WWW 2021有哪些值得读的图机器学习相关论文?
- canvas 实现图片局部模糊_Canvas模糊化处理图片、毛玻璃处理图片之stackblur.js
- 博士哭诉自己入职深大7年,月薪不足3000开不起网课,望学校补助
- 工作总结 EntityFramework中出现DateTime2异常的完美解决办法
- 深度学习(二十七)可视化理解卷积神经网络
- 【电机测速】一文搞懂M法、T法测速原理
- oracle日期英文
- 怎么把录音转文字?只需三步,手把手教会你
- Constraint of Oracle studing
- java能开发硬件程序吗,跳槽薪资翻倍
- 算法手撕代码21~25
- first cdsn day
- 《道德经》第二十三章
- QQ空间伤感日志:泪是我唯一的朋友
- 苹果x和xs买哪个好_买苹果手机好还是安卓手机好?这里有最合适的买法
- python运行脚本被杀死_从python异常中杀死Bash脚本
- 报错:An attempt was made to call a method that does not exist. The attempt was made from the following