【作业】神奇的代码,包装类Integre,100==100,129!=129

 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

就对了。。。

原文地址:https://www.cnblogs.com/CCRNRT/p/9826170.html