StringBuffer,StringBuilder_听韩顺平课笔记

1.StringBuffer结构剖析

StringBuffer基本介绍:

  • StringBuffer中有一个char[] value,用于存储,不是final,因此可以更新内容

StringBuffer,StringBuilder_听韩顺平课笔记

StringBuffer的特点:

StringBuffer,StringBuilder_听韩顺平课笔记

String与StringBuffer的对比:

  • StringBuffer的更新实际上是更新内容,不用每次更新地址(只有在地址不够用时,会扩容)

StringBuffer,StringBuilder_听韩顺平课笔记

2.StringBuffer转换

StringBuffer的构造器:

  • 通过 给一个String 创建StringBuffer,char[] 大小就是str.length() + 16

StringBuffer,StringBuilder_听韩顺平课笔记

StringBuffer和String的相互转换:

String => StringBuffer:

StringBuffer,StringBuilder_听韩顺平课笔记

StringBuffer => String:

StringBuffer,StringBuilder_听韩顺平课笔记

StringBuffer,StringBuilder_听韩顺平课笔记

3.StringBuffer方法

StringBuffer类常见方法:

StringBuffer,StringBuilder_听韩顺平课笔记

StringBuffer,StringBuilder_听韩顺平课笔记

4.StringBuffer练习

StringBuffer练习1:

StringBuffer,StringBuilder_听韩顺平课笔记

练习1答案:

  • 查看append()源码,底层将传入的null转为了字符串"null"
  • 查看构造器public StringBuffer(String str);抛出空指针异常

StringBuffer,StringBuilder_听韩顺平课笔记

练习2:

StringBuffer,StringBuilder_听韩顺平课笔记

练习2答案:

StringBuffer,StringBuilder_听韩顺平课笔记

5.StringBuilder结构剖析

StringBuilder基本介绍:
StringBuilder不是线程安全的,单线程的情况,优先采用StringBuilder

StringBuffer,StringBuilder_听韩顺平课笔记

StringBuilder常用方法:(与StringBuffer基本一样)

StringBuffer,StringBuilder_听韩顺平课笔记

StringBuilder的五个基本特点:

StringBuffer,StringBuilder_听韩顺平课笔记

6.StringBuilder应用

String,StringBuffer和StringBuilder的比较:

StringBuffer,StringBuilder_听韩顺平课笔记

String,StringBuffer和StringBuilder的效率测试:

StringBuffer,StringBuilder_听韩顺平课笔记

String,StringBuffer和StringBuilder的使用原则:

StringBuffer,StringBuilder_听韩顺平课笔记

上一篇:java中String、StringBuffer和StringBuilder的区别


下一篇:14StringBuider