- 浏览: 97269 次
- 性别:
- 来自: 广州
最新评论
文章列表
非常欣赏这个缓存做法。
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));
}
}
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
} ...
一、insert,其实跟replace差不多,都是先通过System.arraycopy Method在字符数组value中预留一个空间,再用多一次这方法插入str或char[]。
1、insert(int index, char str[], int offset,int len)围观源码
// StringBuffer里面,调用父类的方法
public synchronized StringBuffer insert(int index, char str[], int offset,
int l ...
一、replace方法也比较简单。围观源码
public AbstractStringBuilder replace(int start, int end, String str) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (start > count)
throw new StringIndexOutOfBoundsException("start > length()");
if (start > ...
一、append(int i),来自StringBuffer的父类AbstractStringBuilder源码
步骤:
1、围观源码
public AbstractStringBuilder append(int i) {
if (i == Integer.MIN_VALUE) {
append("-2147483648");
return this;
}
// stringSizeOfInt,判断输入的整数i占多少位,如负数+1,方法源码往后看。
...
StringBuffer类继承自AbstractStringBuilder抽象类,实现Serializable序列化接口和CharSequence接口。
AbstractStringBuilder抽象类实现Appendabel,CharSequence接口。
另外,StringBuilder的源码跟StringBuffer类似,看完Buffer再看Builder,找出不同点看看就大致可以。
一,关于append方法浅析。
append方法都做了同步声明,其中源码如下
public synchronized StringBuffer append(String str) {
...