StringBuffer和 StringBuilder
-
StringBuffer :可变长字符串,运行效率慢、线程安全
- append()方法 追加
- insert(int, String) 添加,可以在任意地方
- replace(int start, int end, String) 指定位置替换,含头不含尾
- delete(int start, int end) 删除
- reverse()
-
StringBuilder:可变长字符串,运行效率快、线程不安全
方法和上面一样
wrapper class
- Byte
- Short
- Integer
- Long
- Character
- Float
- Double
- Boolean
装箱:基本转对象
拆箱:对象转基本
编译器会自动转换,完成自动装箱拆箱
分析Integer
-
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构造方法创造,即使在这个范围内,也是不等的