Home >Java >JavaBase >How to determine equality in java

How to determine equality in java

尚
Original
2019-11-23 09:17:393884browse

How to determine equality in java

#To determine whether the basic data types in Java are equal, just use "==" directly. If equal, return true, otherwise, return false.

obj1==obj2 Determine whether the two reference variables obj1 and obj2 are equal, that is, whether the objects they point to are the same object. The implication is that true can only be returned when the memory addresses pointed to by the two variables are equal. Each object has its own piece of memory, so it must point to the same object to return true.

If you want to customize the rules for whether two objects (not one object, that is, the two objects each have their own piece of memory) are equal, you must override the equals() method in the class definition of the object. , if the equals() method is not overridden, the default comparison method is to compare whether the two objects are the same object.

In the Java API, some classes override the equals() method. Their comparison rules are: if and only if the equals method parameter is not null and the type and content of the two variables are the same, compare The result is true.

These classes include: String, Double, Float, Long, Integer, Short, Byte, Boolean, BigDecimal, BigInteger, etc. There are too many, but these are the common ones. You can check the API for details. Just look at the equals() method in the class.

The steps for rewriting the equals() method are generally as follows:

1. First use "==" to determine whether they are equal.

2. Determine whether the parameter of the equals() method is null. If it is null, return false; because the current object cannot be null. If it is null, its equals() method cannot be called, otherwise it will throw java.lang.NullPointerException exception.

3. When the parameter is not null, if the runtime classes of the two objects (obtained through getClass()) are not equal, return false, otherwise continue to judge.

4. Determine whether the members of the class are equal. Just feel free to play with it.

Example:

public class Dog {
    private int age;
    private String name;
    public Dog(int age,String name){
        this.age=age;
        this.name=name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public boolean equals(Object obj) {
        if(obj==null){
            return false;
        }
        if(this==obj){
            return true;
        }
        if(obj instanceof Dog){
            Dog dog=(Dog)obj;
            if(dog.age==this.age&&dog.name.equals(this.name)){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }
    public static void main(String[] args){
        Dog dog1=new Dog(12,"wangcai");
        Dog dog2=new Dog(12,"wangcai");
        Dog dog3=new Dog(16,"wangcai");
        System.out.println(dog1.equals(dog2));//true
        System.out.println(dog1.equals(dog3));//false
    }}

For more java knowledge, please pay attention to java basic tutorial.

The above is the detailed content of How to determine equality in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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