java中基本类型与装箱基本类型“==”比较出现的几种情况

java中基本类型与装箱基本类型“==”比较出现的几种情况

java有一个类型系统有两部分组成,包含基本类型(primitive),例如:int、double等,还有引用类型(reference type),例如:String、List。每个基本类型都有一个对应的引用类型,称为装箱基本类型(boxed promitive)。装箱基本类型中对应于int、double的是Integer、Double。

Java 1.5发行版本中增加了自动装箱和自动拆箱,自动装箱和自动拆箱就是我们所知道的“语法糖”,这些特性模糊了但并没有完全抹去基本类型和装箱基本类型之间的区别。这两种类型之间真正是有差别的,要很清楚的使用的是哪种类型,并且要对这两种类型进行谨慎的选择,这些都非常重要。

在基本类型和装箱类型之间有三个主要区别。

  • 第一:基本类型只有值,而装箱基本类型则具有与他们的值不同的同一性。换句话说,两个装箱基本类型可以具有相同的值和不同的同一性。
  • 第二:基本类型只有功能完备的值,而每个装箱基本类型除了它对应基本类型的所有功能值之外,还有个非功能值:null。
  • 第三:基本类型通常比装箱基本类型更节省时间和空间。

如果你不小心上面的三点,这三点区别都会让你陷入麻烦之中。

先看一个简单的例子程序

如下

package com.wrh.firstpro;

public class EqualsDemo06 {

    public static void main(String[] args) {
        System.out.println(myCompare(new Integer(4),new Integer(4)));
    }
    public static int myCompare(Integer i1,Integer i2){

        return i1<i2?-1:((i1==i2)?0:1);
    }

}

程序的输出结果是:1
但是这两个Integer实例都是表示相同的值4,为什么函数myCompare返回的是1,而不是我们想象中的0呢?
原因如下:此分析参考《Effective Java中文版》第49条

对表达式 i1<i2执行计算会导致first和second引用的Integer实例被自动拆箱;也就是说,它提取了他们的基本类型值。计算动作要检查产生的第一个int值是否小于第二个。答案是否定的。下一个测试就是执行计算表达式i1==i2,它在两个对象上执行同一性的比较。如果i1和i2引用表示同一个int值的不同的Integer的实例,这个比较操作就会返回false,因此此函数错误的返回:1;

但是,我将这个文件生成的.class文件反编译后,代码如下:

package com.wrh.firstpro;

import java.io.PrintStream;

public class EqualsDemo06
{
  public static void main(String[] paramArrayOfString)
  {
    System.out.println(myCompare(new Integer(4), new Integer(4)));
  }

  public static int myCompare(Integer paramInteger1, Integer paramInteger2) {
    return paramInteger1 == paramInteger2 ? 0 : paramInteger1.intValue() < paramInteger2.intValue() ? -1 : 1;
  }
}

发现,编译器将return i1<i2?-1:((i1==i2)?0:1);变成了

return paramInteger1 == paramInteger2 ? 0 : paramInteger1.intValue() < paramInteger2.intValue() ? -1 : 1;

因此,我们根据反编译后的代码分析运行结果:1的原因如下:先进行两个Integer实例的同一性分析,不相等,然后通过Integer.intValue()将两个Integer实例自动拆箱来比较大小,由于这两个Integer实例表示相同的数值,因此,得到结果1

修正上面这个问题的方法如下:
添加两个int型的中间变量,在这些中间变量上执行所有的比较操作可以避免同一性的比较。

public static int myCompare(Integer i1,Integer i2){
        int temp1=i1;
        int temp2=i2;
        return temp1<temp2?-1:((temp1==temp2)?0:1);
    }

对封装基本类型引用==操作符几乎总是错误的

第一个例子程序:两个都是通过new出来的Integer的实例进行“==”比较

package com.wrh.firstpro;

public class EqualsDemo02 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Integer i7=new Integer(4);
        Integer i8=new Integer(4);
        System.out.println("i7==i8?"+(i7==i8));
        System.out.println("i7.equals(i8)?"+i7.equals(i8));
    }

}

运行结果如下:

i7==i8?false
i7.equals(i8)?true

反编译的代码如下:

public class EqualsDemo02
{
  public static void main(String[] paramArrayOfString)
  {
    Integer localInteger1 = new Integer(4);
    Integer localInteger2 = new Integer(4);
    System.out.println(new StringBuilder().append("i7==i8?").append(localInteger1 == localInteger2).toString());
    System.out.println(new StringBuilder().append("i7.equals(i8)?").append(localInteger1.equals(localInteger2)).toString());
  }
}

产生这个的原因在于应用“==”对两个new出来的Integer类型的实例进行比较时,即时表示相同的数值,由于Integer类型的数据进行比较还要进行同一性的比较,因此不相等。而equals的比较结果是与我们的预期是一样的。

总结:应用“==”对两个new出来的Integer类型的实例进行比较时,即时表示相同的数值,由于Integer类型的数据进行比较还要进行同一性的比较,因此不相等

第二个例子程序:两个通过自动装箱得到的Integer实例进行“==”比较

在继续往下面来看

package com.wrh.firstpro;

public class EqualsDemo03 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Integer i3=3;
        Integer i4=3;
        System.out.println(i3==i4);
        System.out.println(i3.equals(i4));

    }

}

运行结果如下:

true
true

与上一个例子相比的区别在于将

Integer i7=new Integer(4);
Integer i8=new Integer(4);

换成了

Integer i3=3;
Integer i4=3;

就导致了该程序的运用==比较时,判断是相等的,即Integer i3=3;Integer i4=3;使得i3、i4的同一性的相同的。
总结:通过“==”比较两个自动装箱的Integer实例时,只要两者所表示的数值时相同的,结果就是相等的,因此他们的同一性是相同的。

上面例子的反编译代码如下


public class EqualsDemo03
{
  public static void main(String[] paramArrayOfString)
  {
    Integer localInteger1 = Integer.valueOf(3);
    Integer localInteger2 = Integer.valueOf(3);
    System.out.println(localInteger1 == localInteger2);
    System.out.println(localInteger1.equals(localInteger2));
  }
}

第三个例子程序:一个是通过new出来的Integer实例,一个是自动装箱而得到的Integer实例进行“==”比较

程序如下:

package com.wrh.firstpro;

public class EqualsDemo07 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Integer i1=1;
        Integer i2=new Integer(1);
        System.out.println(i1==i2);
        System.out.println(i2.equals(i1));

    }

}

程序运行结果如下:

false
true

结果比较好理解,这里不再分析。

第四个例子程序:“==”左右两边都是Integer实例存在运算符运算的比较

继续来看例子

package com.wrh.firstpro;

public class EqualsDemo04 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Integer i1=1;
        Integer i2=2;
        Integer i3=3;
        System.out.println(i3==(i1+i2));
        System.out.println(i3.equals(i1+i2));

    }

}

运行结果如下

true
true

即上面的比较都是先进行自动拆箱后然后进行数值上大小的比较

通过反编译后的代码相信就可以很好的分析出答案了,代码如下

public class EqualsDemo04
{
  public static void main(String[] paramArrayOfString)
  {
    Integer localInteger1 = Integer.valueOf(1);
    Integer localInteger2 = Integer.valueOf(2);
    Integer localInteger3 = Integer.valueOf(3);
    System.out.println(localInteger3.intValue() == localInteger1.intValue() + localInteger2.intValue());
    System.out.println(localInteger3.equals(Integer.valueOf(localInteger1.intValue() + localInteger2.intValue())));
  }
}

总结:包装类的“==”运算在遇到算术运算的情况下会自动拆箱,进行其代表的数值大小的比较

第五个例子程序

package com.wrh.firstpro;

public class EqualsDemo05 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Integer i6=4;
        Long    i5=4L;
        //System.out.println(i5==i6);//不能编译,Integer类型和Long类型不能用“==”比较
        System.out.println(i6.equals(i5));
    }

}

运行结果是:false,这个结果比较好理解

反编译后的代码如下:

public class EqualsDemo05
{
  public static void main(String[] paramArrayOfString)
  {
    Integer localInteger = Integer.valueOf(4);
    Long localLong = Long.valueOf(4L);

    System.out.println(localInteger.equals(localLong));
  }
}

总结

(1)反编译的代码中Integer.valueOf()和Integer.intValue()进行了自动装箱和自动拆箱。
(2)应用“==”对两个new出来的Integer类型的实例进行比较时,即时表示相同的数值,由于Integer类型的数据进行比较还要进行同一性的比较,因此不相等
(3)通过“==”比较两个自动装箱的Integer实例时,只要两者所表示的数值时相同的,结果就是相等的,因此他们的同一性是相同的。
(4)通过equals来对来比较的时候,只要同类型(包括能自动装箱和拆箱)代表的数值时相同的,就是相等的。

其它类型与Integer类型的自动装箱和拆箱的原理相同,这里不再赘述。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值