包装类Integre

public class StrangeIntegerBehavior 
{ 
    public static void main(String[] args)
    {
        Integer i1=100;
        Integer j1=100;
        System.out.println(i1==j1);//true

        Integer i2=129;  
        Integer j2=129;
        System.out.println(i2==j2);//false
    
    }
}

两对整数明明一模一样,为什么一个输出true一个输出false

安装jd-eclipse对该程序反编译如下

// Compiled from StrangeIntegerBehavior.java (version 10 : 54.0, super bit)
public class StrangeIntegerBehavior {
  
  // Method descriptor #6 ()V
  // Stack: 1, Locals: 1
  public StrangeIntegerBehavior();
    0  aload_0 [this]
    1  invokespecial java.lang.Object() [8]
    4  return
      Line numbers:
        [pc: 0, line: 1]
      Local variable table:
        [pc: 0, pc: 5] local: this index: 0 type: StrangeIntegerBehavior
  
  // Method descriptor #15 ([Ljava/lang/String;)V
  // Stack: 3, Locals: 5
  public static void main(java.lang.String[] args);
     0  bipush 100
     2  invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [16]
     5  astore_1 [i1]
     6  bipush 100
     8  invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [16]
    11  astore_2 [j1]
    12  getstatic java.lang.System.out : java.io.PrintStream [22]
    15  aload_1 [i1]
    16  aload_2 [j1]
    17  if_acmpne 24
    20  iconst_1
    21  goto 25
    24  iconst_0
    25  invokevirtual java.io.PrintStream.println(boolean) : void [28]
    28  sipush 129
    31  invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [16]
    34  astore_3 [i2]
    35  sipush 129
    38  invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [16]
    41  astore 4 [j2]
    43  getstatic java.lang.System.out : java.io.PrintStream [22]
    46  aload_3 [i2]
    47  aload 4 [j2]
    49  if_acmpne 56
    52  iconst_1
    53  goto 57
    56  iconst_0
    57  invokevirtual java.io.PrintStream.println(boolean) : void [28]
    60  return
      Line numbers:
        [pc: 0, line: 5]
        [pc: 6, line: 6]
        [pc: 12, line: 7]
        [pc: 28, line: 9]
        [pc: 35, line: 10]
        [pc: 43, line: 11]
        [pc: 60, line: 13]
      Local variable table:
        [pc: 0, pc: 61] local: args index: 0 type: java.lang.String[]
        [pc: 6, pc: 61] local: i1 index: 1 type: java.lang.Integer
        [pc: 12, pc: 61] local: j1 index: 2 type: java.lang.Integer
        [pc: 35, pc: 61] local: i2 index: 3 type: java.lang.Integer
        [pc: 43, pc: 61] local: j2 index: 4 type: java.lang.Integer
      Stack map table: number of frames 4
        [pc: 24, full, stack: {java.io.PrintStream}, locals: {java.lang.String[], java.lang.Integer, java.lang.Integer}]
        [pc: 25, full, stack: {java.io.PrintStream, int}, locals: {java.lang.String[], java.lang.Integer, java.lang.Integer}]
        [pc: 56, full, stack: {java.io.PrintStream}, locals: {java.lang.String[], java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer}]
        [pc: 57, full, stack: {java.io.PrintStream, int}, locals: {java.lang.String[], java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer}]
}
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)里的,但是如果将代码改一下

public static void main(String[] args)
    {
        Integer i1=100;
        Integer j1=100;
        System.out.println(i1==j1);//true

        Integer i2=12;  
        Integer j2=12;
        System.out.println(i2==j2);//true
    
    }

结果为:

true

true

自我抑郁又自我救赎
原文地址:https://www.cnblogs.com/zjm15511858030/p/9827752.html