JAVA--Lang包常用类
1.1 Integer
对int类型的数据的包装。 private final int value; 维护Integer的数据
public final class Integer extends Number implements Comparable<Integer>
static int MAX_VALUE
static int MIN_VALUE
Integer(int value)
Integer(String s) 将字符串转整型数据
==装箱: 基本类型 转换成 包装类型的对象 包装类.valueOf()==
==拆箱: 将包装类对象转换成基本类型的数据==
private static void demo2() {
int num = 100;
Integer num1 = 100;
// Integer num2 = Integer.valueOf(100);
// 默认是int 基本类型 转换成 包装类型的对象 包装类.valueOf(基本类型 变量)
//拆箱: 将包装类对象转换成基本类型的数据
//System.out.println(num == num1.intValue());
System.out.println(num == num1);//true
}
==整数缓存池 (容器:数组)==
public static Integer valueOf(int i) {//100 200
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
//i low---high 直接获得是数组的指定索引的元素数据 -128--127
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() {}
}
Byte---ByteCache Short----ShortCache Long---LongCache Character----CharacterCache(0-127)
parseInt
public static int parseInt(String s, int radix)
throws NumberFormatException
{
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}
int result = 0;//最后转换的数值结果
boolean negative = false;//正数 负整数
int i = 0, len = s.length();//获得字符串的字符个数
int limit = -Integer.MAX_VALUE;
int multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);// charAt(index) 获得字符串指定索引的字符的数据
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
digit = Character.digit(s.charAt(i++),radix);//10
//将一个指定字符内容转换成指定进制内的数字
//digit = 1
//digit = 2
//digit = 3
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;// result = result*radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;// result = result-digit; -123
}
} else {
throw