1. 程式人生 > >【作業】神奇的代碼,包裝類Integre,100==100,129!=129

【作業】神奇的代碼,包裝類Integre,100==100,129!=129

man gets virtual 緩存 ima time esp ret numbers

 1 public class StrangeIntegerBehavior 
 2 { 
 3     public static void main(String[] args)
 4     {
 5         Integer i1=100;
 6         Integer j1=100;
 7         System.out.println(i1==j1);//true
 8 
 9         Integer i2=129;  
10         Integer j2=129;
11         System.out.println(i2==j2);//false
12
13 } 14 }

問題:兩對整數明明一模一樣,為什麽一個輸出true一個輸出false

安裝jd-eclipse對該程序反編譯如下

 1 // Compiled from StrangeIntegerBehavior.java (version 10 : 54.0, super bit)
 2 public class StrangeIntegerBehavior {
 3   
 4   // Method descriptor #6 ()V
 5   // Stack: 1, Locals: 1
 6   public StrangeIntegerBehavior();
 7     0  aload_0 [this
] 8 1 invokespecial java.lang.Object() [8] 9 4 return 10 Line numbers: 11 [pc: 0, line: 1] 12 Local variable table: 13 [pc: 0, pc: 5] local: this index: 0 type: StrangeIntegerBehavior 14 15 // Method descriptor #15 ([Ljava/lang/String;)V 16 // Stack: 3, Locals: 5
17 public static void main(java.lang.String[] args); 18 0 bipush 100 19 2 invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [16] 20 5 astore_1 [i1] 21 6 bipush 100 22 8 invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [16] 23 11 astore_2 [j1] 24 12 getstatic java.lang.System.out : java.io.PrintStream [22] 25 15 aload_1 [i1] 26 16 aload_2 [j1] 27 17 if_acmpne 24 28 20 iconst_1 29 21 goto 25 30 24 iconst_0 31 25 invokevirtual java.io.PrintStream.println(boolean) : void [28] 32 28 sipush 129 33 31 invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [16] 34 34 astore_3 [i2] 35 35 sipush 129 36 38 invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [16] 37 41 astore 4 [j2] 38 43 getstatic java.lang.System.out : java.io.PrintStream [22] 39 46 aload_3 [i2] 40 47 aload 4 [j2] 41 49 if_acmpne 56 42 52 iconst_1 43 53 goto 57 44 56 iconst_0 45 57 invokevirtual java.io.PrintStream.println(boolean) : void [28] 46 60 return 47 Line numbers: 48 [pc: 0, line: 5] 49 [pc: 6, line: 6] 50 [pc: 12, line: 7] 51 [pc: 28, line: 9] 52 [pc: 35, line: 10] 53 [pc: 43, line: 11] 54 [pc: 60, line: 13] 55 Local variable table: 56 [pc: 0, pc: 61] local: args index: 0 type: java.lang.String[] 57 [pc: 6, pc: 61] local: i1 index: 1 type: java.lang.Integer 58 [pc: 12, pc: 61] local: j1 index: 2 type: java.lang.Integer 59 [pc: 35, pc: 61] local: i2 index: 3 type: java.lang.Integer 60 [pc: 43, pc: 61] local: j2 index: 4 type: java.lang.Integer 61 Stack map table: number of frames 4 62 [pc: 24, full, stack: {java.io.PrintStream}, locals: {java.lang.String[], java.lang.Integer, java.lang.Integer}] 63 [pc: 25, full, stack: {java.io.PrintStream, int}, locals: {java.lang.String[], java.lang.Integer, java.lang.Integer}] 64 [pc: 56, full, stack: {java.io.PrintStream}, locals: {java.lang.String[], java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer}] 65 [pc: 57, full, stack: {java.io.PrintStream, int}, locals: {java.lang.String[], java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer}] 66 }

invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [16]
這裏調用了靜態類java.lang.Integer.valueOf(int),查看源碼得

valueOf

public static Integer valueOf(int i)
Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor 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.
Parameters:
i - an int value.
Returns:
an Integer instance representing i.
Since:
1.5
重點在於This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.(該方法總是將值緩存在-128到127(包括在內)範圍內,並且可能會緩存超出此範圍的其他值。)
i1和j1都是符合範圍得數,而i2和j2大於128超出了範圍
然後在還有一句話
Returns an Integer object holding the value of the specified String. The argument is interpreted as representing a signed decimal integer, exactly as if the argument were given to the parseInt(java.lang.String) method. The result is an Integer object that represents the integer value specified by the string.

In other words, this method returns an Integer object equal to the value of:

new Integer(Integer.parseInt(s))
翻譯:返回包含指定字符串值的整數對象。該參數被解釋為表示一個帶符號的十進制整數,就像該參數被賦給parseInt(java.lang.String)方法一樣。結果是一個整數對象,表示字符串指定的整數值。換句話說,這個方法返回一個整數對象等於:新的整數(Integer.parseInt(s))
所以後面兩個i2和j2不相等,因為他們是new出來的。
但這句話是寫在java.lang.Integer.valueOf(String)裏的,但是如果將代碼改一下
 1 public static void main(String[] args)
 2     {
 3         Integer i1=100;
 4         Integer j1=100;
 5         System.out.println(i1==j1);//true
 6 
 7         Integer i2=12;  
 8         Integer j2=12;
 9         System.out.println(i2==j2);//true
10     
11     }

結果為:

true

true

就對了。。。

【作業】神奇的代碼,包裝類Integre,100==100,129!=129