StringBudiler源码简单解析
StringBudiler源码
- 继承关系树
- 底层实现
- 默认容量()
- 特别的添加方法(append)
1、继承关系树
继承自AbstractStringBuilder与StringBuffer同族
2、底层实现
//基本大部分方法都是调用父类的方法,点进父类查看
public StringBuilder() {
super(16);
}
AbstractStringBuilder.java
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
底层和String一样使用字符串实现,都是包装类型
有参构造函数长度扩充为len+16;StringBuilder(CharSequence seq) {}同理
public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}
3、默认容量
public StringBuilder() {
super(16);
}
默认容量16
使用有参构造器返回的容量为str.length()+16
public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}
4、添加方法
@Override
public StringBuilder append(String str) {
super.append(str);
return this;
}
//调用父类方法
查看父类的方法
public AbstractStringBuilder append(String str) {
if (str == null)
return appendNull();
int len = str.length();
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
- 如果值为空,使用appendNull()是不是有个疑问,如果需要添加的值为空时不是直接返回当前StringBuilder就行了吗,为什么还要调用一个方法呢?
appendNull()方法
private AbstractStringBuilder appendNull() {
int c = count;
ensureCapacityInternal(c + 4);
final char[] value = this.value;
value[c++] = 'n';
value[c++] = 'u';
value[c++] = 'l';
value[c++] = 'l';
count = c;
return this;
}
- 判断容量是否足够,如果不够通过Arrays.copy复制到新的字符串数组中然后将'null'添加到StringBuilder中
回到append(String str)方法
//扩容
ensureCapacityInternal(count + len);
//value为StringBuilder中实际拥有的char[],这里是讲str复制到value也就是StringBuilder里面去
str.getChars(0, len, value, count);
//变更实际长度
count += len;
return this;
append(StringBuffer sb)方法
public AbstractStringBuilder append(StringBuffer sb) {
}
来看一下使用append(String str)方法添加一个空字符串
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
System.out.println("字符串第一个值:"+sb+" ; 长度:"+sb.length());
String str = null;
sb.append(str);
System.out.println("修改后:"+sb+" ; 长度:"+sb.length());
}
测试结果:
字符串第一个值: ; 长度:0
修改后:null ; 长度
至于为什么是添加'null',这里就存疑,有知道的小伙伴可以在评论中回复
StringBudiler源码简单解析的更多相关文章
- node-pre-gyp以及node-gyp的源码简单解析(以安装sqlite3为例)
title: node-pre-gyp以及node-gyp的源码简单解析(以安装sqlite3为例) date: 2020-11-27 tags: node native sqlite3 前言 简单来 ...
- springmvc(2)Controller源码简单解析
前面简单的分析了一下DispatcherServlet,接下来分析一下Controller,在web的MVC中,Controller就是其中的C,启动的一些页面逻辑处理,页面映射的功能: 首先看看超类 ...
- Okhttp源码简单解析(一)
业余时间把源码clone下来大致溜了一遍,并且也参阅了其余大神的博客,在这里把自己的心得记录下来共享之,如有不当的地方欢迎批评指正.本文是Okttp源码解析系列的第一篇,不会深入写太多的东西,本篇只是 ...
- ForkJoinPool源码简单解析
ForkJoin框架之ForkJoinTask java 阅读约 62 分钟 前言 在前面的文章"CompletableFuture和响应式编程"中提到了ForkJoinTas ...
- springmvc(1)DispatcherServlet源码简单解析
springmvc的简单配置 1.首先需要在web.xml中配置DispatcherServlet,这个类是springmvc的核心类,所以的操作都是由这里开始,并且大部分都是在这里面实现的,比如各种 ...
- FFmpeg的HEVC解码器源码简单分析:解析器(Parser)部分
===================================================== HEVC源码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpeg ...
- iOS开发之Masonry框架源码深度解析
Masonry是iOS在控件布局中经常使用的一个轻量级框架,Masonry让NSLayoutConstraint使用起来更为简洁.Masonry简化了NSLayoutConstraint的使用方式,让 ...
- Retrofit源码设计模式解析(下)
本文将接着<Retrofit源码设计模式解析(上)>,继续分享以下设计模式在Retrofit中的应用: 适配器模式 策略模式 观察者模式 单例模式 原型模式 享元模式 一.适配器模式 在上 ...
- Android 图片加载框架Glide4.0源码完全解析(二)
写在之前 上一篇博文写的是Android 图片加载框架Glide4.0源码完全解析(一),主要分析了Glide4.0源码中的with方法和load方法,原本打算是一起发布的,但是由于into方法复杂性 ...
随机推荐
- 【转】JPG打包压缩后比原来尺寸还大
作者:刘源链接:https://www.zhihu.com/question/40371280/answer/86262934来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...
- 迷你MVVM框架 avalonjs 0.83发布
本版本做了如下改进: 重构计算属性, 这是@soom提出的BUG,发现计算属性被某个监控属性向上驱动更新自己时,不会解发$watch回调.详见这里. 强化ms-bind绑定,当第一次扫描时也会执行它的 ...
- 使用 phpStorm 开发
苦恼蛋疼的曾哥工作室,让人痛不欲生,缓慢的同步速度,另人恼火的插件配置,让人疯狂的卡.简直是让人用了几天之后就不行了. 废话不多说,一款很好的php IDE. 1. phpStorm 下载 here ...
- 小学生轻松做题App
作业链接:https://edu.cnblogs.com/campus/fzzcxy/2016SE/homework/2180 原型模型设计工具:墨刀 原型模型链接:https://modao.cc/ ...
- 12. Integer to Roman (HashTable)
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- mac下git push避免每次都输入用户名和密码的配置
参考链接:http://www.linuxdiyf.com/linux/18389.html 链接2:https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%8 ...
- NOSQL之Redis、MongDB、Habase、Cassandra的介绍与比较
一.Redis介绍 1.1Redis优点 (1)Redis拥有非常丰富的数据结构: (2)Redis提供事务的功能,可以保证一串命令的原子性,中间不会被任何打断. (3)数据存储在内存中,读写 ...
- 74款安卓和IOS app源码地址
知乎专栏App https://github.com/bxbxbai/ZhuanLan WeChat高仿微信 项目地址: https://github.com/motianhuo/wechat Gan ...
- android模拟按键问题总结[使用IWindowManager.injectKeyEvent方法](转)
http://blog.csdn.net/xudongdong99/article/details/8857173 Android上面TreeView效果 http://blog.csdn.net/g ...
- oj1089-1096总结(输入输出练习)
//无限输出类 #include<stdio.h>int main(void){ int a,b; while((scanf("%d %d",&a,&b ...