Home > Java > javaTutorial > body text

What is the difference between using == and equals in Java

PHPz
Release: 2023-05-15 09:25:11
forward
2442 people have browsed it

1, == Analysis

== Often used for comparisons between the same basic data types, and can also be used for comparisons between objects of the same type;

  • If == is comparing basic data types, then the comparison is whether the values ​​​​of the two basic data types are equal;

  • If == are two objects being compared, then what is being compared is the references of the two objects, then it is to compare whether the references of the two objects are equal, that is, to determine whether the two objects point to the same memory area. ;

2. Equals method analysis

equals method is mainly used between two objects to detect whether one object is equal to another object.

Let’s take a look at the source code of the equals method in the Object class

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

Its function is also to determine whether two objects are equal. There are generally two usage situations:

Situation 1: The equals method of the object has not been overridden, then when equals is called, it compares whether the references of the two objects are equal, that is, the two Whether the object points to the same memory area. At this time, it is equivalent to == comparing two objects.

Dog class

package com.xiao;

/**
 * @author :小肖
 * @date :Created in 2022/3/11 14:42
 */
public class Dog {

    private String name;

    private Integer age;


    public Dog() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Dog(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}
Copy after login

Test class

import com.xiao.Dog;

public class Test {

    public static void main(String[] args) {
        Dog dog = new Dog("小旺",2);
        Dog dog1 = new Dog("小旺",2);
        System.out.println(dog.equals(dog1));
    }
}
Copy after login

Test result

false

Case 2: Object equals method has been overridden. Generally, our rewritten equals method compares whether the contents of two objects are equal. If they are equal, return true, otherwise return false.

Dog class

package com.xiao;

/**
 * @author :小肖
 * @date :Created in 2022/3/11 14:42
 */
public class Dog {

    private String name;

    private Integer age;


    public Dog() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Dog(String name, Integer age) {
        this.name = name;
        this.age = age;
    }


    @Override
    public boolean equals(Object obj) {
        if(obj.getClass() != getClass()){
            return false;
        }
        Dog dog = (Dog) obj;
        if(dog.getAge() == age && dog.getName().equals(name)){
            return true;
        }
        return false;
    }
}
Copy after login

Test result

The test class code is the same as above

true

3 , The equals method has the following characteristics

  • Reflexivity: For any reference value x that is not null, x.equals(x) must be true.

  • Symmetry: For any non-null reference values ​​x and y, y.equals(x) is also true if and only if x.equals(y) is true.

  • Transitivity: For any non-null reference values ​​x, y and z, if x.equals(y) is true and y.equals(z) is true, then x.equals(z) must be true.

  • Consistency: For any non-null reference values ​​x and y, if the object information used for equals comparison has not been modified, x.equals(y) will be called multiple times Either consistently returns true or consistently returns false.

  • For any reference value x that is not null, x.equals(null) returns false .

The above is the detailed content of What is the difference between using == and equals in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template