本篇文章基于jdk1.8
概述
Integer类定义如下:
public final class Integer extends Number implements Comparable<Integer> {
}
它是一个final修饰的类,意味着它不可被继承,也提供了相对比较完善的功能。
Integer类是原始类型int的包装类,一个Integer类的对象包含有一个单独的类型是int的字段。这个类提供了几种将int转换为String和String转换为int的方法,以及处理int时有用的其他常量和方法。
bit twiddling方法比如highestOneBit(int)方法和numberOfTrailingZeros(int)方法的实现都是基于 Henry S. Warren, Jr.'s提供的材料
Integer类的一个成员变量是:
/**
* The value of the {@code Integer}.
*
* @serial
*/
private final int value;
这个成员变量value即是Integer类的int值的存储变量。
Integer类的最大最小值定义如下
/**
* 最小值:-2147483648
*/
@Native
public static final int MIN_VALUE = 0x80000000;
/**
* 最大值:2147483647
*/
@Native
public static final int MAX_VALUE = 0x7fffffff;
parseInt方法
parseInt方法源代码当中最重要的实现逻辑如下:
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;
可以看到实现的方法是通过String类的charAt方法去获取字符,然后传入类Character的digit方法当中。
equals方法
Integer类重写了equals方法,实现如下:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
即比较两个Integer对象的int值是否相等。
Integer类的缓存实现
在看Integer类的缓存之前不妨先来看一段代码
public class IntegerTest {
public static void main(String[] args) {
Integer int1 = Integer.valueOf("100");
Integer int2 = Integer.valueOf("100");
System.out.println(int1 == int2);
Integer int3 = Integer.valueOf("1000");
Integer int4 = Integer.valueOf("1000");
System.out.println(int3 == int4);
}
}
这段代码的打印结果为:
true
false
第二个打印结果为false可以理解,毕竟比较的是对象,那么为什么第一个比较的结果却是true呢,答案就在Integer类的缓存里面。在Integer类当中有一个内部类用于实现缓存。
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
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() {}
}
以上代码的作用就是,使用了一个静态cache数组,初始化时数组将一定范围的整数放到cache数组中。因此在使用Integer类的时候,它已经把-128到127的整数都提前实例化了。 这就解释了上面的例子程序的结果,不管创建多少个这个范围内的Integer用ValueOf出来的都是同一个对象。
因为ValueOf方法的实现就用到了这个缓存,如下:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
一目了然,若传入的参数i在cahce的最大值和最小值之间,那么就直接返回cache当中的值,省去了实例化的步骤。
bitCount方法
该方法的目的是就算一个十进制的数对应的二进制数当中1的个数,实现如下:
public static int bitCount(int i) {
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}