StringBuffer :可变长字符串,运行效率慢、线程安全
StringBuilder:可变长字符串,运行效率快、线程不安全
方法和上面一样
装箱:基本转对象
拆箱:对象转基本
编译器会自动转换,完成自动装箱拆箱
public int intValue()
将对象转化为int值
public static Integer valueOf(int i)
将int值转换为Integer对象
public static int parseInt(String s)
字符串转换为Int值
IntegerCache
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
? 会初始化-128到127,通过valueOf创建在这个范围内是相等的(复用)
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
但是如果是用Integer构造方法创造,即使在这个范围内,也是不等的
StringBuffer和 StringBuilder和包装类
原文:https://www.cnblogs.com/zxy-GO/p/13939758.html