int和Integer的区别
区别 | int | Integer |
---|---|---|
类型 | 基本数据类型 | 引用类型 |
默认值 | null |
现象
int 和 Integer == 比较
public void test1() {
Integer i1 = 100;
int j1 = 100;
Integer i2 = 200;
int j2 = 200;
System.out.println(i1 == j1); // true
System.out.println(i2 == j2); // true
}
追本溯源:javap -c TestInteger.class
public void test1();
Code:
0: bipush 100
2: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: astore_1
6: bipush 100
8: istore_2
9: sipush 200
12: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
15: astore_3
16: sipush 200
19: istore 4
21: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
24: aload_1
25: invokevirtual #4 // Method java/lang/Integer.intValue:()I
28: iload_2
29: if_icmpne 36
32: iconst_1
33: goto 37
36: iconst_0
37: invokevirtual #5 // Method java/io/PrintStream.println:(Z)V
40: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
43: aload_3
44: invokevirtual #4 // Method java/lang/Integer.intValue:()I
47: iload 4
49: if_icmpne 56
52: iconst_1
53: goto 57
56: iconst_0
57: invokevirtual #5 // Method java/io/PrintStream.println:(Z)V
60: return
Integer 和 Integer == 比较
public void test2() {
Integer i1 = 100;
Integer j1 = 100;
Integer i2 = 200;
Integer j2 = 200;
System.out.println(i1 == j1); // true
System.out.println(i2 == j2); // false
}
追本溯源:javap -c TestInteger.class
public void test2();
Code:
0: bipush 100
2: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: astore_1
6: bipush 100
8: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
11: astore_2
12: sipush 200
15: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
18: astore_3
19: sipush 200
22: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
25: astore 4
27: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
30: aload_1
31: aload_2
32: if_acmpne 39
35: iconst_1
36: goto 40
39: iconst_0
40: invokevirtual #5 // Method java/io/PrintStream.println:(Z)V
43: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
46: aload_3
47: aload 4
49: if_acmpne 56
52: iconst_1
53: goto 57
56: iconst_0
57: invokevirtual #5 // Method java/io/PrintStream.println:(Z)V
60: return
int 和 new Integer == 比较
public void test3() {
Integer i1 = new Integer(100);
int j1 = 100;
Integer i2 = new Integer(200);
int j2 = 200;
System.out.println(i1 == j1); // true
System.out.println(i2 == j2); // true
}
追本溯源:javap -c TestInteger.class
public void test3();
Code:
0: new #6 // class java/lang/Integer
3: dup
4: bipush 100
6: invokespecial #7 // Method java/lang/Integer."<init>":(I)V
9: astore_1
10: bipush 100
12: istore_2
13: new #6 // class java/lang/Integer
16: dup
17: sipush 200
20: invokespecial #7 // Method java/lang/Integer."<init>":(I)V
23: astore_3
24: sipush 200
27: istore 4
29: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
32: aload_1
33: invokevirtual #4 // Method java/lang/Integer.intValue:()I
36: iload_2
37: if_icmpne 44
40: iconst_1
41: goto 45
44: iconst_0
45: invokevirtual #5 // Method java/io/PrintStream.println:(Z)V
48: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
51: aload_3
52: invokevirtual #4 // Method java/lang/Integer.intValue:()I
55: iload 4
57: if_icmpne 64
60: iconst_1
61: goto 65
64: iconst_0
65: invokevirtual #5 // Method java/io/PrintStream.println:(Z)V
68: return
Integer 和 new Integer == 比较
public void test4() {
Integer i1 = new Integer(100);
Integer j1 = 100;
Integer i2 = new Integer(200);
Integer j2 = 200;
System.out.println(i1 == j1); // false
System.out.println(i2 == j2); // false
}
追本溯源:javap -c TestInteger.class
public void test4();
Code:
0: new #6 // class java/lang/Integer
3: dup
4: bipush 100
6: invokespecial #7 // Method java/lang/Integer."<init>":(I)V
9: astore_1
10: bipush 100
12: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
15: astore_2
16: new #6 // class java/lang/Integer
19: dup
20: sipush 200
23: invokespecial #7 // Method java/lang/Integer."<init>":(I)V
26: astore_3
27: sipush 200
30: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
33: astore 4
35: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
38: aload_1
39: aload_2
40: if_acmpne 47
43: iconst_1
44: goto 48
47: iconst_0
48: invokevirtual #5 // Method java/io/PrintStream.println:(Z)V
51: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
54: aload_3
55: aload 4
57: if_acmpne 64
60: iconst_1
61: goto 65
64: iconst_0
65: invokevirtual #5 // Method java/io/PrintStream.println:(Z)V
68: return
Integer 和 new Integer equals 比较
public void test5() {
Integer i1 = new Integer(100);
Integer j1 = 100;
Integer i2 = new Integer(200);
Integer j2 = 200;
System.out.println(i1.equals(j1)); // true
System.out.println(i2.equals(j2)); // true
}
追本溯源:javap -c TestInteger.class
public void test5();
Code:
0: new #6 // class java/lang/Integer
3: dup
4: bipush 100
6: invokespecial #7 // Method java/lang/Integer."<init>":(I)V
9: astore_1
10: bipush 100
12: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
15: astore_2
16: new #6 // class java/lang/Integer
19: dup
20: sipush 200
23: invokespecial #7 // Method java/lang/Integer."<init>":(I)V
26: astore_3
27: sipush 200
30: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
33: astore 4
35: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
38: aload_1
39: aload_2
40: invokevirtual #8 // Method java/lang/Integer.equals:(Ljava/lang/Object;)Z
43: invokevirtual #5 // Method java/io/PrintStream.println:(Z)V
46: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
49: aload_3
50: aload 4
52: invokevirtual #8 // Method java/lang/Integer.equals:(Ljava/lang/Object;)Z
55: invokevirtual #5 // Method java/io/PrintStream.println:(Z)V
58: return
知识点
自动装箱拆箱
自动装箱
Integer i = 100; // Integer.valueOf(100)
自动拆箱
integer.intValue()
Integer源码核心方法
Integer.valueOf
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
equals
/**
* Compares this object to the specified object. The result is
* {@code true} if and only if the argument is not
* {@code null} and is an {@code Integer} object that
* contains the same {@code int} value as this object.
*
* @param obj the object to compare with.
* @return {@code true} if the objects are the same;
* {@code false} otherwise.
*/
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
扩展
java中基本类型的包装类的大部分都实现了常量池技术,Byte,Short,Integer,Long,Character,Boolean 常量池技术 使用设计模式:享元模式 实现。
Byte,Short,Integer,Long,Character 这5种整型的包装类也只是在对应值小于等于127时才可使用对象池,也即对象不负责创建和管理大于127的这些类的对象
具体可以查看基本类型对应包装类的源码。
最佳实践
- 当 int 与 Integer 进行 == 比较时,JVM会将 Integer 进行自动拆箱。
- 基本类型参与比较时,可以使用 ==
- 两个对象类型比较时,可以使用 equals() 比较数值
- 两个对象类型比较时,可以使用 == 比较是否为同一个对象
Github代码案例
了解更多 java基础:目录索引