• 【java基础 15】java代码中“==”和equals的区别


    导读:昨夜闲来无事,和贾姑娘聊了聊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.

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


  • 相关阅读:
    Spring Boot 自定义 Banner 教程
    Spring Boot 操作 Excel
    Spring Boot 文件上传简易教程
    SpringMVC 处理请求的整个流程?入口?
    实现单例模式的9个方法
    Mybatis Generator最完整配置详解
    接口限流
    添加jar包到本地Maven仓库
    Java ConcurrentModificationException异常原因和解决方法
    RESTful API 设计指南[转]
  • 原文地址:https://www.cnblogs.com/hhx626/p/7534609.html
Copyright © 2020-2023  润新知