Java学习笔记之equals方法

Object类的equals方法

Object类中的源码:

public boolean equals(Object obj) {
            return this == obj;
        }

Object obj:表示可以传递任何对象
==:比较运算符,可以返回true或者false
基本数据类型:比较的是值
引用数据类型:比较的是两个对象的地址

public class Test {
    public static void main(String[] args) {
        Person one = new Person("小明",20);
        Person two = new Person("小花",30);
        //Person类继承Object类,可以使用Object类的equals方法。
        /*public boolean equals(Object obj) {
            return this == obj;
        }
        Object obj:表示可以传递任何对象
        ==:比较运算符,可以返回true或者false
        基本数据类型:比较的是值
        引用数据类型:比较的是两个对象的地址
        this是谁?谁调用的方法,this就是谁。比如one调用的方法,this就是one;
        */
        System.out.println(one);
        System.out.println(two);
        System.out.println(one.equals(two));
        one = two;
        System.out.println("分割线================");
        System.out.println(one);
        System.out.println(two);
        System.out.println(one.equals(two));

    }
}

结果

"E:\JAVA\IntelliJ IDEA 2019.3.2\jbr\bin\java.exe" "-javaagent:E:\JAVA\IntelliJ IDEA 2019.3.2\lib\idea_rt.jar=55736:E:\JAVA\IntelliJ IDEA 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath E:\JAVA\project\day06-code\out\production\day06-code com.ls.demo05.Test
com.ls.demo05.Person@2d98a335
com.ls.demo05.Person@16b98e56
false
分割线================
com.ls.demo05.Person@16b98e56
com.ls.demo05.Person@16b98e56
true

Process finished with exit code 0

重写Object类的equals方法

Object的类的equals方法默认比较的是两个对象的地址值。重写equals,比较两个对象的属性。此处隐含一个多态,多态无法使用子类特有的内容。Object obj = two =new Person("小花",30);可以向下转型将Obj转化为Person。
在Person类中重写equals方法。
可以通过IDEA快速添加:Alt+Ins再选择equals() and hashCode()

 @Override
    public boolean equals(Object o) {
        if (this == o) return true;//提高效率
        if (!(o instanceof Person)) return false;//防止类型转换异常
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }
 @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

结果:

"E:\JAVA\IntelliJ IDEA 2019.3.2\jbr\bin\java.exe" "-javaagent:E:\JAVA\IntelliJ IDEA 2019.3.2\lib\idea_rt.jar=55956:E:\JAVA\IntelliJ IDEA 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath E:\JAVA\project\day06-code\out\production\day06-code com.ls.demo05.Test
com.ls.demo05.Person@165f3d6
com.ls.demo05.Person@165f3d6
true
分割线================
com.ls.demo05.Person@165f3d6
com.ls.demo05.Person@165f3d6
true

Process finished with exit code 0

Objects类的equals方法

在JDK7添加了一个Objects工具类,它提供了一些方法来操作对象,它由一些静态的实用方法组成,这些方法是null-save (空指针安全的)或null-tolerant (容忍空指针的) , 用于计算对象的hashcode.返回对象的字符串表示形式、比较两个对象。

public class Test {
    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = "AbC";
        System.out.println(str1.equals(str2));
        String str3 = null;
        String str4 = "AbC";
        //System.out.println(str3.equals(str4));//NullPointerException
        System.out.println(Objects.equals(str1,str2));
        System.out.println(Objects.equals(str3,str4));//此时无空指针异常
    }
}
发布了22 篇原创文章 · 获赞 0 · 访问量 426

猜你喜欢

转载自blog.csdn.net/ENDEAVOR__/article/details/105499758