Skip to main content

One post tagged with "Java"

View All Tags

· 4 min read
CkaiGrac

一、设置合理的初始长度

在StringBuilder的源码中,有一个char[ ]数组,这个就是用来存储字符的。

//AbstractStringBuilder.java
/**
* The value is used for character storage.
*/
char[] value;
/**
* The count is the number of characters used.
*/
int count;

而count统计字符数量。当直接new StringBuilder();时,传递到父类的默认大小(capacity)为16,也就是默认状态下char[ ]数组的长度为16。 来看一下append方法: