Skip to main content

One post tagged with "Java"

View All Tags

StringBuilder的高性能使用方式总结

· 3 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 方法: