目录
toHexString,toOctalString,toBinaryString
三个比较方法,compareTo,compare,compareUnsigned
简介
Integer类是java原始类型int的包装类
/**
* <p>Integer类包装了一个原始类型int的值在一个对象中。
* 一个Integer类的对象包含了一个单一的int类型的字段。
*
* <p>此外,这个类提供了几个将int转为String和将String转为int的方法,
* 还有一些在处理int类型时有用的常量和方法。
*
* <p>实现的注解:位处理的实现方法(例如highestOneBit,numberOfTrailingZeros,numberOfTrailingZeros)
* 是基于Henry S. Warren, Jr.'s Hacker's Delight的材料。
*
* @author Lee Boynton
* @author Arthur van Hoff
* @author Josh Bloch
* @author Joseph D. Darcy
* @since JDK1.0
*/
public final class Integer extends Number implements Comparable<Integer>
字段
MIN_VALUE,MAX_VALUE
/**
* 一个持有int类型的最小值的常量,-2<sup>31</sup>,-2147483648
*/
@Native public static final int MIN_VALUE = 0x80000000;
//1后面3+7*4=31个0,对应2的31次方(1后面3个0对应1000对应8对应2的3次方)
/**
* 一个持有int类型的最大值的常量,2<sup>31</sup>-1,2147483647
*/
@Native public static final int MAX_VALUE = 0x7fffffff;
//3+7*4=31个1,对应2的31次方-1(3个1对应111对应7对应2的3次方-1)
TYPE
/**
* 代表着原始类型int的Class实例,类似int.class 原始类
*
* @since JDK1.1
*/
@SuppressWarnings("unchecked")
public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
digits
/**
* 所有可能作为一个String代表数字的char,0-9和a-z,因为可能出现2-35进制。
*/
final static char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
DigitTens,DigitOnes
/**这两个char数组是给getChars方法调用的
*
*/
final static char [] DigitTens = {
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
} ;
final static char [] DigitOnes = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
} ;
sizeTable
/**
* 根据数组里面的值,进行大小判断,从而得到10进制下,有几位
*/
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
99999999, 999999999, Integer.MAX_VALUE };
value
/**
* <p>Integer的值
*
* <p>注意它是private,final的,
* 别人不能对它修改,只能换一个Integer对象
*
* <p>Integer最重要的值,代表了这个Integer对象。
*
* @serial
*/
private final int value;
SIZE,BYTES
/**
* 用二进制补码形式表示int值的位数
*
* @since 1.5
*/
@Native public static final int SIZE = 32;
/**
* 用二进制补码形式表示整数值的字节数。
* 一个字节为8个bit,一个int总共4个byte,32个bit
*
* @since 1.8
*/
public static final int BYTES = SIZE / Byte.SIZE;
产生Integer的方法(构造类与各种valueOf)
Integer缓存
/**
* <p>缓存,支持由JLS要求的-128到127(包含)的值的自动装箱的对象语义标识。
*
* <p>缓存在第一次使用时初始化。缓存的大小由选项 {@code -XX:AutoBoxCacheMax=<size>} 控制。
* 在虚拟机初始化时,值IntegerCache.high可以被类sun.misc.VM中的私有系统属性设置并保存。
*
* <p>注意;它是私有的static内部类,只能由Integer类访问
*/
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
//第一次加载这个类的时候执行下面代码
static {
// high 值能够通过属性配置,但是默认是127
int h = 127;
//从sun.misc.VM得到high
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
//如果设置了的话,127<=high<=Integer.MAX_VALUE - 129
//即如果不超过上下限,即为设置的值,否则为上下限
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// 数组最大的长度是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;
//根据high和low创建integer数组cache
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
//cache中下标0对应-128,length-1对应high
cache[k] = new Integer(j&#