Integer类继承Number类,实现Comparable接口
Number是一个抽象类,里面定义了对应int,float等数据类型的多种抽象方法
Integer的parseInt方法:
首先对参数进行判断,如果s为null,抛出异常
如果radix小于Character.MIN_RADIX(值为2)抛出异常
因为在ASCII码中,‘+’和‘-’都位于‘0’前,如果第一个字符小于‘0’,那么就对这个字符进行判断,看它是正的还是负的
所以String中不能出现大于或等于radix的数,否则抛出异常
110为例
计算方式result=1*10*10+1*10+0(radix=10)
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);
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) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
valueOf方法返回的是一个Integer对象,parseInt返回的是一个int
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
Integer.intValue()返回的是一个int,所以相对于对两个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;
}
可以看到缓存设置在-128到127之间,这也就可以解释两个Integer对象在value相等的情况下
在-128在127之间Integer==Integer2返回true
而超过这个范围返回false
Number是一个抽象类,里面定义了对应int,float等数据类型的多种抽象方法
Integer的parseInt方法:
parseInt(String s)默认调用parseInt(s,10)
parseInt(String s,int radix):radix为String表示的数是以几进制的形式表现的
首先对参数进行判断,如果s为null,抛出异常
如果radix小于Character.MIN_RADIX(值为2)抛出异常
如果radix大于Character.MAX_RADIX(值为36)抛出异常
取出s的第一个字符,判断是否小于零因为在ASCII码中,‘+’和‘-’都位于‘0’前,如果第一个字符小于‘0’,那么就对这个字符进行判断,看它是正的还是负的
所以String中不能出现大于或等于radix的数,否则抛出异常
110为例
计算方式result=1*10*10+1*10+0(radix=10)
result=1*2*2+1*2+0
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);
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) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
valueOf方法返回的是一个Integer对象,parseInt返回的是一个int
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
equals方法:
重写了Object的equals方法
public boolean equals(Object obj) {if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
Integer.intValue()返回的是一个int,所以相对于对两个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;
}
可以看到缓存设置在-128到127之间,这也就可以解释两个Integer对象在value相等的情况下
在-128在127之间Integer==Integer2返回true
而超过这个范围返回false
而又因为equals方法返回的始终是int型,所以只要数值相同,那么就为true