导读:昨夜闲来无事,和贾姑娘聊了聊java基础,然后就说到了这个“==”和equals的问题,我俩都是以前了解过,也常用这个,但是,昨天说到的时候,又乱了,什么比较地址值,什么判断是否同一个对象,基本数据类型啥的,谁都没个准儿,后来写了点代码验证了一番,今儿个写此博客,纪念和好朋友一起探讨学习的经历!PS:我一直知道我这一路走来,受好朋友的恩惠太多了!

一、三组示例代码

1.1,String类(引用类型)

String str1="test";
String str2="test";
//true true
System.out.println(str1==str2);
System.out.println(str1.equals(str2));
String str3=new String("test");
String str4=new String("test");
//false true
System.out.println(str3==str4);
System.out.println(str3.equals(str4));
String str5=str1;
//true true
System.out.println(str5==str1);
System.out.println(str5.equals(str1));
String str6=str3;
//false true
System.out.println(str6==str1);
System.out.print(str6.equals(str1));

刚开始,统一的意见是“==”是比较值,equals是比较引用对象,请看下面的经典论断(主要是针对str3和str4):

A:==比值,这两个值都是test,应该是true,可结果是false,后面那个equals比较对象,两个都是new的,应该不一样,可结果是true

B:==比值,因为这两个不是简单类型,无法比较,所以返回false;equals 比较对象,此时str3 和str4新创建了两个字符串对象,这两个对象是一样的,返回true

A:如果是byte、int、boolean、long,float,这些的话,==就是比较值,equals比对象,是吧

B:对

1.2,Integer类(int的包装类,非8种基本类型)

Integer int1=1;
Integer int2=1;
//true true
System.out.println(int1==int2);
System.out.println(int1.equals(int2)); Integer int3=new Integer(1);
Integer int4=new Integer(1);
//false true
System.out.println(int3==int4);
System.out.println(int3.equals(int4)); Integer int5=int1;
//true true
System.out.println(int5==int1);
System.out.println(int5.equals(int1)); Integer int6=int3;
//true true
System.out.println(int6==int3);
System.out.print(int6.equals(int3));

结果和String类一致!

1.3,int(基本类型)

备注:用简单基本类型,根本无法使用equals方法,只能用==,对于String类或者简单类型的包装类(Integer是int的包装类)==比较的是否是同一个地址(对象),equals比较的是地址值

二、分析equals方法

所有的对象都是继承于Object类,那么首先看Object里面,关于equals的定义:

   * @param   obj   the reference object with which to compare.
     * @return  {@code true} if this object is the same as the obj
     *          argument; {@code false} otherwise.
     * @see     #hashCode()
     * @see     java.util.HashMap
     */
    public boolean equals(Object obj) {
        return (this == obj);
    }

从这里可以看出,equals事实上比较的是否是同一个对象目标,也就是是否是同一个内存地址。但从这个角度来说,那么就无法解释为什么在代码段1中str3.equals(str4)的结果为true了。因为这两个对象都采用了new 关键字,在堆中,必定会存在两个空间地址。为了解决这个问题,势必要去看看String类对于equals方法的重写:

/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}

从代码可以看出,String类对于equals方法进行了改写,当String类使用equals的时候,if and only if the argument is not null and isa object that represents the same sequence of characters
as this object
. 对比Object基类返回true的条件:if this object is the same as the obj argument。 现在再去看代码段1中的str3.equals(str4)的结果为true,就能理解了!

接下来,再看基本类型的包装类中对于equals方法的改写:

   /**
* Compares this object to the specified object. The result is
* {@code true} if and only if the argument is not
* {@code null} and is an {@code Integer} object that
* contains the same {@code int} value as this object.
*
* @param obj the object to compare with.
* @return {@code true} if the objects are the same;
* {@code false} otherwise.
*/
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}

if and only if the argument is not null and is an object that contains the same value as this object. 从这里可以看出,它的改写,并未要求是同一对象,而要求是同一值!

附:更为明显的代码片段

Integer intA=new Integer(1);
Integer intB=new Integer(1);
Integer intC=1;
//false false true true
System.out.println(intA==intB);
System.out.println(intA==intC);
System.out.println(intA.equals(intB));
System.out.println(intA.equals(intC));

三、总结

写这篇博客,主要是想纪念一下和好朋友之间的探讨交流,感觉和大家一起交流一些简单又有意思的事儿,真心很好玩。而且感觉我现在还比较喜欢看源码,也就来源于对这些小东西的兴趣和好奇。当然目前的学习程度是远远不够的,正在努力中。对了,以上关于equals的各类源码,属于java 8.

多看代码,多交流,多总结,希望自己保持下去,然后和大家一起成长!

【java基础 15】java代码中“==”和equals的区别的更多相关文章

  1. Java基础:Object类中的equals与hashCode方法

    前言 这个系列的文章主要用来记录我在学习和复习Java基础知识的过程中遇到的一些有趣好玩的知识点,希望大家也喜欢. 一切皆对象   对于软件工程来说面向对象编程有一套完整的解决方案:OOA.OOD.O ...

  2. Java基础-使用JAVA代码剖析MD5算法实现过程

    Java基础-使用JAVA代码剖析MD5算法实现过程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  3. Java基础语法(8)-数组中的常见排序算法

    title: Java基础语法(8)-数组中的常见排序算法 blog: CSDN data: Java学习路线及视频 1.基本概念 排序: 是计算机程序设计中的一项重要操作,其功能是指一个数据元素集合 ...

  4. 黑马程序员:Java基础总结----java注解

    黑马程序员:Java基础总结 java注解   ASP.Net+Android+IO开发 . .Net培训 .期待与您交流! java注解 lang包中的基本注解 @SuppressWarnings ...

  5. java基础(二)-----java的三大特性之继承

    在<Think in java>中有这样一句话:复用代码是Java众多引人注目的功能之一.但要想成为极具革命性的语言,仅仅能够复制代码并对加以改变是不够的,它还必须能够做更多的事情.在这句 ...

  6. Java基础技术-Java其他主题【面试】

    Java基础技术-Java其他主题[面试] Java基础技术IO与队列 Java BIO.NIO.AIO Java 中 BIO.NIO.AIO 的区别是什么? 含义不同: BIO(Blocking I ...

  7. Java基础:Java的四种引用

    在Java基础:java虚拟机(JVM)中,我们提到了Java的四种引用.包括:强引用,软引用,弱引用,虚引用.这篇博客将详细的讲解一下这四种引用. 1. 强引用 2. 软引用 3. 弱引用 4. 虚 ...

  8. java基础-学java util类库总结

    JAVA基础 Util包介绍 学Java基础的工具类库java.util包.在这个包中,Java提供了一些实用的方法和数据结构.本章介绍Java的实用工具类库java.util包.在这个包中,Java ...

  9. Java基础15:深入剖析Java枚举类

    更多内容请关注微信公众号[Java技术江湖] 这是一位阿里 Java 工程师的技术小站,作者黄小斜,专注 Java 相关技术:SSM.SpringBoot.MySQL.分布式.中间件.集群.Linux ...

随机推荐

  1. BLL-IDAL-DAL的关系

    BLL  实现 IDAL  是一个接口 DAL 实现方法 BLL 调用IDAL 的方法 IDAL中的方法   在 DAL中必须实现 使用的方法  调用BLL的方法就可以

  2. UVA12906 Maximum Score (组合)

    对于每个元素,最理想的情况就是都在它的左边或者右边,那么sort一下就可以得到一个特解了,然后大的中间不能有小的元素,因为如果有的话,那么无论选小的还是选大的都不是最优.对小的元素来说,比它大的元素在 ...

  3. ABNF语法

    http典型的请求场景 来自极客时间课件 http协议介绍 . [c:\~]$ telnet www.taohui.pub 80 Host 'www.taohui.pub' resolved to 1 ...

  4. inner join 和 left join 的区别

    1.left join.right join.inner join的区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表 ...

  5. 解决 cocos2dx iOS/mac 设置纹理寻址模式后纹理变黑的问题

    sprite:getTexture():setTexParameters(gl.LINEAR,gl.LINEAR,gl.REPEAT,gl.REPEAT) 在安卓设备上,设置了纹理自定义寻址模式,纹理 ...

  6. [bzoj]3436 小K的农场

    [题目描述] 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述:农场a比农场b至少多种植了c个单位的 ...

  7. Ping 命令的执行过程和应用协议

    1. ICMP是“Internet Control Message Ptotocol”的缩写.它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制消息. 控制消息是指网络通不通.主机 ...

  8. 转 Spring源码剖析——核心IOC容器原理

    Spring源码剖析——核心IOC容器原理 2016年08月05日 15:06:16 阅读数:8312 标签: spring源码ioc编程bean 更多 个人分类: Java https://blog ...

  9. kali下安装中文输入法

    参考网址:https://blog.csdn.net/qq_37367124/article/details/79229739 更性源 vim /etc/apt/source.list 设置更新源 更 ...

  10. asp发送短信验证码 pst方式

    <script language="jscript" runat="server">  Array.prototype.get = function ...