一、Character的缓存设计
二、方法源码解析
2、Character方法的源码浅析。
private final char value;
private static class CharacterCache {
private CharacterCache(){}
// 只缓存127+1个字符
static final Character cache[] = new Character[127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Character((char)i);
}
}
二、方法源码解析
2、Character方法的源码浅析。
// valueOf 小于127的在缓存中查找,超过就new
public static Character valueOf(char c) {
if(c <= 127) { // must cache
return CharacterCache.cache[(int)c];
}
return new Character(c);
}
public String toString() {
// 为什么要buffer呢?用意何在,直接String.valueOf(value)不可以吗?
char buf[] = {value};
return String.valueOf(buf);
}
public static int codePointAt(CharSequence seq, int index) {
char c1 = seq.charAt(index++);
if (isHighSurrogate(c1)) {
if (index < seq.length()) {
char c2 = seq.charAt(index);
if (isLowSurrogate(c2)) {
return toCodePoint(c1, c2);
}
}
}
return c1;
}