Integer简述
Integer是JDK1.5出现的int原生类型的包装类型,在JDK9之前版本,通常是使用构造函数的方式创建对象:
Integer integer = new Integer(1);
从JDK9开始之后,通过构造方法这种方式创建Integer对象的方式被抛弃,使用Integer提供的静态方法来创建对象:
Integer integer = Integer.valueOf(1);
Integer为用户创建integer对象提供了基于int和String的两种类型的方式:
下面主要讨论的基于jdk11的源码进行分析。
Integer基本属性
//Integer可以装箱的int最小值,采用16进制表示
@Native public static final int MIN_VALUE = 0x80000000;
//Integer可以装箱的int最大值
@Native public static final int MAX_VALUE = 0x7fffffff;
//实际保存原生int的值
private final int value;
//表示的Integer所表示的int类型占32位
@Native public static final int SIZE = 32;
//表示int的字节数
public static final int BYTES = SIZE / Byte.SIZE;
//所有的数子和字母字符
static final 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’