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

2024-05-14 05:18

本文主要是介绍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 stubInteger 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 stubInteger 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 stubInteger 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 stubInteger 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 stubInteger 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类型的自动装箱和拆箱的原理相同,这里不再赘述。

这篇关于java中基本类型与装箱基本类型“==”比较出现的几种情况的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/987850

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd