public class Demo {
public static void main(String[] args) {
Integer a = 126;
Integer b = 126;
Integer c = new Integer(126);
System.out.println(a.equals(b));
System.out.println(a == c);
System.out.println(a.equals(c));
}
}
控制台输出结果
true
false
true
javap -verbose Demo.class
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=3, locals=4, args_size=1
0: bipush 126
2: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: astore_1
6: bipush 126
8: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
11: astore_2
12: new #3 // class java/lang/Integer
15: dup
16: bipush 126
18: invokespecial #4 // Method java/lang/Integer."<init>":(I)V
21: astore_3
22: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
25: aload_1
26: aload_2
27: invokevirtual #6 // Method java/lang/Integer.equals:(Ljava/lang/Object;)Z
30: invokevirtual #7 // Method java/io/PrintStream.println:(Z)V
33: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
36: aload_1
37: aload_3
38: if_acmpne 45
41: iconst_1
42: goto 46
45: iconst_0
46: invokevirtual #7 // Method java/io/PrintStream.println:(Z)V
49: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
52: aload_1
53: aload_3
54: invokevirtual #6 // Method java/lang/Integer.equals:(Ljava/lang/Object;)Z
57: invokevirtual #7 // Method java/io/PrintStream.println:(Z)V
60: return
LineNumberTable:
line 11: 0
line 12: 6
line 13: 12
line 14: 22
line 15: 33
line 16: 49
line 17: 60
LocalVariableTable:
Start Length Slot Name Signature
0 61 0 args [Ljava/lang/String;
6 55 1 a Ljava/lang/Integer;
12 49 2 b Ljava/lang/Integer;
22 39 3 c Ljava/lang/Integer;
StackMapTable: number_of_entries = 2
frame_type = 255 /* full_frame */
offset_delta = 45
locals = [ class "[Ljava/lang/String;", class java/lang/Integer, class java/lang/Integer, class java/lang/Integer ]
stack = [ class java/io/PrintStream ]
frame_type = 255 /* full_frame */
offset_delta = 0
locals = [ class "[Ljava/lang/String;", class java/lang/Integer, class java/lang/Integer, class java/lang/Integer ]
stack = [ class java/io/PrintStream, int ]
MethodParameters:
Name Flags
args
}
Integer 缓存
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
private static class IntegerCache {
static final int low = -128;
static final int high;
static {
// high value may be configured by property
int h = 127;
....
}
}
- 本文链接: http://maltose.top/archives/integer-huan-cun
- 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!