- Integer是int的包装类, int是基本数据类型
最主要的区别就是这句 一个是基本数据类型, 一个是类,
但其实其中隐含的细节很多,比如:
- Integer的默认初始值是null,而int的默认初试值是0。
- Integer变量必须先实例,int变量则可以直接使用
因为Integer是包装的类, 所以自带很多方法可以使用, (方法表附在最后),
而int是基本数据类型只能做一些基本的+-*/=等操作 ,当二者交叉就会出现很多问题,
下面是一些Integer 的用法和注意事项:
- “==”和equals():
在Integer类中,“==”用来比较对象地址是否相同,并且Integer类重写了equals(Object obj)方法,在equals(Object obj)方法中,会先判断参数中的对象obj是否是Integer类型的对象,如果是则判断值是否相同,值相同则返回true,值不同则返回false,如果obj不是Integer类的对象,则返回false。需要注意的是:当参数是基本类型int时,编译器会给int自动装箱成Integer类,然后再进行比较。
既然提到了拆箱和装箱, 装箱就是由基本数据类型封装成类的过程, 反而反之
那么什么时候会拆箱?——基本数据类型和引用数据类型做运算时
什么时候会装箱?——基本数据类型赋值给引用数据类型的时候
- Integer变量实际上是对一个Integer对象的引用,所以两个通过new生成的Integer变量永远是不相等的(new生成的是两个对象,其内存地址不同)。
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.print(i == j);
//false
- Integer变量和int变量比较时,只要两个变量的值是向等的,则结果为true(包装类Integer和基本数据类型int比较时,java会自动拆包装为int,然后进行比较,实际上就变为两个int变量的比较)
Integer i = new Integer(1); int j = 1; System.out.print(i == j); //true
- 非new生成的Integer变量和new Integer()生成的变量比较时,结果为false。(因为非new生成的Integer变量指向的是java常量池中的对象,而new Integer()生成的变量指向堆中新建的对象,两者在内存中的地址不同)
Integer i = new Integer(1);
Integer j = 1;
System.out.print(i == j);
//false
-
对于两个非new生成的Integer对象,进行比较时,如果两个变量的值在区间-128到127之间,则比较结果为true,如果两个变量的值不在此区间,则比较结果为false
Integer i = 1; Integer j = 1; System.out.print(i == j); //true
这条乍看起来绝对是蒙蔽的, 一样的方式怎么有的时候返回真有的时候为假呢?Integer i = 128; Integer j = 128; System.out.print(i == j); //false
因为JVM中一个字节一下的整型数据(即[128,127])会在JVM启动时加载进内存,除非用new Integer()显示的创建对象,否则都是同一对象。
Integer i = 1时,会将1进行缓存,下次再写Integer j = 1时,就会直接从缓存中取,就不会new了, 这样取到的1 地址也是相同的, ==判断返回真,
对于-128到127范围之外的数 实际上就是new得到的, 地址不同==判断为假
对于这点感兴趣的可以看看这个解释:
- java在编译Integer i = 100 ;时,会翻译成为Integer i = Integer.valueOf(100);
所以关键就是看valueOf()函数了。JDK源码的valueOf()是这样的:
public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) { return IntegerCache.cache[i + (-IntegerCache.low)]; } return new Integer(i); }
Integer的 属性和方法:
构造函数 |
---|
Integer(int value) 构造一个 |
Integer(String s) 构造一个新分配的 |
修饰符和类型 | 域和描述 |
---|---|
static int | MAX_VALUE 保持最大值的常数 |
static int | MIN_VALUE 保持最小值的常数 |
static int | SIZE 用于表示 |
static Class<Integer> | TYPE
|
方法摘要
修饰符和类型 | 方法和描述 |
---|---|
static int | bitCount(int i) 返回指定 |
byte | byteValue() 返回此值 |
static int | compare(int x, int y) 用 |
int | compareTo(Integer anotherInteger) 用 |
static Integer | decode(String nm) 将a解码 |
double | doubleValue() 返回此值 |
boolean | equals(Object obj) 将此对象与指定的对象进行比较。 |
float | floatValue() 返回此值 |
static Integer | getInteger(String nm) 确定具有指定名称的系统属性的整数值。 |
static Integer | getInteger(String nm, int val) 确定具有指定名称的系统属性的整数值。 |
static Integer | getInteger(String nm, Integer val) 返回具有指定名称的系统属性的整数值。 |
int | hashCode() 返回此哈希码 |
static int | highestOneBit(int i) 返回一个 |
int | intValue() 返回此值 |
long | longValue() 返回此值 |
static int | lowestOneBit(int i) 返回一个 |
static int | numberOfLeadingZeros(int i) 返回指定 |
static int | numberOfTrailingZeros(int i) 返回指定 |
static int | parseInt(String s) 将字符串参数解析为带符号的十进制整数。 |
static int | parseInt(String s, int radix) 将字符串参数解析为第二个参数指定的基数中的有符号整数。 |
static int | reverse(int i) 返回通过反转指定 |
static int | reverseBytes(int i) 返回通过反转指定 |
static int | rotateLeft(int i, int distance) 返回通过旋转 |
static int | rotateRight(int i, int distance) 返回通过将指定 |
short | shortValue() 返回此值 |
static int | signum(int i) 返回指定 |
static String | toBinaryString(int i) 返回整数参数的字符串表示形式,作为base 2中的无符号整数。 |
static String | toHexString(int i) 返回整数参数的字符串表示形式,作为基数为16的无符号整数。 |
static String | toOctalString(int i) 返回整数参数的字符串表示形式,作为基数为8的无符号整数。 |
String | toString() 返回 |
static String | toString(int i) 返回 |
static String | toString(int i, int radix) 返回第二个参数指定的基数中第一个参数的字符串表示形式。 |
static Integer | valueOf(int i) 返回 |
static Integer | valueOf(String s) 返回包含 |
static Integer | valueOf(String s, int radix) 返回一个 |
详细请看:
https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html