天天看点

需要注意自动装拆箱的一个特例

首先看一段代码(使用JDK 5),如下: public class Test {

  public static void main(String[] args) {

    Integer i1 = 127;

    Integer i2 = 127;       if (i1 == i2)

      System.out.println("Equal!");

    else

      System.out.println("Not equal!");

  }

}       输出结果想必大家也知道,是“Equal!”。现在把i1和i2的值由127改为128看看会发生什么?结果输出“Not equal!”。   提示:这段解释有错误,可以跳过看下面的。        注意i1和i2都是Integer类型,事实上只要这个值的范围在“-128—127”之间,输出结果都是“Equal!”。JDK 5引进了很多新的特性,其中有一个就是自动装箱(Autoboxing)和自动拆箱(Auto-Unboxing)。当i1和i2值为128时,在进行“==”时,它们被装进两个不同的Integer Objects,由于这是两个不同的instances,它们引用不同的内存地址,所以结果是“Not equal!”。 但当这个值是127时,JVM自动将这个值转换成基本类型int,这样进行“==”时,JVM仍然使用的是相同的object instance, 所以输出结果为“Equal!”了。          真是讨厌!

修改后的:

首先感谢abcdhy网友指出问题,为了更能鞭策自己,我会将上面错误的解释一直留着,时刻提醒自己,研究问题一定要脚踏实地,多方考证。       为了方便说明问题,我写了下面的代码:   public class Test {

  public static void main(String[] args) {

    Integer i1 = 127;

    Integer i2 = 127;

    Integer i3 = Integer.valueOf(127);       if (i1 == i2)

      System.out.println("i1 == i2 is true!");

    else

      System.out.println("i1 == i2 is false!");       if (i1 >= i2)

      System.out.println("i1 >= i2 is true!");

    else

      System.out.println("i1 >= i2 is false!");       if (i1 == i3)

      System.out.println("i1 == i3 is true!");

    else

      System.out.println("i1 == i3 is false!");     }

}

当值是127时,输出是: i1 == i2 is true!

i1 >= i2 is true!

i1 == i3 is true!   当值是128时,输出是: i1 == i2 is false!

i1 >= i2 is true!

i1 == i3 is false!   说明:    我使用的是Sun JDK 1.5.0_03-b07 和 Eclipse 3.2M4。       “Integer i1 = 127;”在JDK1.4下不能编译通过的,会提示:“ Type mismatch: cannot convert from int to Integer”的错误,一般改写为:“Integer i1 = new Integer(127);”。        “Integer i1 = 127;”在JDK1.5下可以编译通过的,这就是自动装箱(Autoboxing)和自动拆箱(Auto-Unboxing)。自动装箱(Autoboxing)特性让Java自动包装一个简单数据类型(例如int)到对应的包装类型中(例如Integer)中。       在《 JSR 201: Extending the Java Programming Language with Enumerations, Autoboxing, Enhanced for loops and Static Import》中,对这个问题,是作了这样的规定:

    If the value p being boxed is true, false, a byte, an ASCII character, or an integer or short number between -127 and 128, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

      在Java中,The following is the list of primitives stored as immutable objects(不可变对象 ): * boolean values true and false

* All byte values

* short values between -128 and 127

* int values between -128 and 127

* char in the range /u0000 to /u007F      为了更容易理解问题,用Jad将上面代码反编译,如下: import java.io.PrintStream;   public class Test

{       public Test()

    {

    }       public static void main(String args[])

    {

        Integer i1 = Integer.valueOf(128);

        Integer i2 = Integer.valueOf(128);

        Integer i3 = Integer.valueOf(128);

        if(i1 == i2)

            System.out.println("i1 == i2 is true!");

        else

            System.out.println("i1 == i2 is false!");

        if(i1.intValue() >= i2.intValue())

            System.out.println("i1 >= i2 is true!");

        else

            System.out.println("i1 >= i2 is false!");

        if(i1 == i3)

            System.out.println("i1 == i3 is true!");

        else

            System.out.println("i1 == i3 is false!");

    }

}

从这可以看出,“Integer i1 = 127;”在JDK1.5下应该编译成了“Integer i1 = Integer.valueOf(127);”。     再看看java.lang.Integer中关于valueOf的源代码是怎样的:   public static Integer valueOf(int i) {

    final int offset = 128;

    if (i >= -128 && i <= 127) { // must cache

      return IntegerCache.cache[i + offset];

    }

    return new Integer(i);

  }       可以看出,这个值在-128到127之间,会将其cached(缓存)起来,如果多次使用的话,会节省内存和改善性能;如果不在这个范围之内,则生成一个新的Integer Object instance,这样如果进行“==”时,由于是比较两个不同的Object references,故结果是false。事实上,这个特性从JDK 1.3就存在了(以前的我不清楚) 。       确切的说,上面出现的问题实质并不是Autoboxing和Auto-Unboxing,应该是Integer类的valueOf(int i)方法,只是Autoboxing和Auto-Unboxing将这个问题掩盖了。