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
}
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
}
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
}
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
}
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
常量池技術 使用設計模式:享元模式 實作。
最佳實踐
- 當 int 與 Integer 進行 == 比較時,JVM會将 Integer 進行自動拆箱。
- 基本類型參與比較時,可以使用 ==
- 兩個對象類型比較時,可以使用 equals() 比較數值
- 兩個對象類型比較時,可以使用 == 比較是否為同一個對象