Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

WangScaler: A writer with heart.

Declaration: uneducated, if there is a mistake, kindly correct.

Basic variables, etc

We know that == is the comparison operator to determine equality, and = is an assignment statement, so we use == to determine whether two variables are equal. In fact, == can only be used to judge the basic variables.

public class EqualsDemo {
    public static void main(String[] args) {
        int a = 10;
        int b = 11 - 1;
        System.out.println(a == b);//true}}Copy the code

Determination of reference types

A reference type that uses the == judgment does not get the desired result, because == compares the memory addresses it references. Some people say that when you use Integer, sometimes you can call it equal success, sometimes you can’t.

public class EqualsDemo {
    public static void main(String[] args) {
        Integer a = 1;
        Integer b = 1;
        Integer c = 129;
        Integer d = 129;
        System.out.println(a == b);//true
        System.out.println(c == d);//false}}Copy the code

The reason for this is constant pooling in Java. When an Integer is between -127 and 128, it is retrieved from an array (the above constant pool) by IntegerCache, while anything beyond the range is rewritten. Equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals You can also see this in my previous article Java Error # 2.

public class EqualsDemo {
    public static void main(String[] args) {
        Integer a = 1;
        Integer b = 1;
        Integer c = 129;
        Integer d = 129;
        System.out.println(a.equals(b));//true
        System.out.println(c.equals(d));//true}}Copy the code

The pit of Equals

The most common type of exception we’re starting out with is null-pointer exceptions, and equals will also pit null-pointer exceptions. Our object is User

class User {
    private int id;
    private String name;
​
    public int getId(a) {
        return id;
    }
​
    public void setId(int id) {
        this.id = id;
    }
​
    public String getName(a) {
        return name;
    }
​
    public void setName(String name) {
        this.name = name; }}Copy the code

You can see that the object has only two parameters, id and name. Next we read the data from the database, which only has id and name is null. We simulate such a piece of data, not write from the database read.

User user = new User();
user.setId(1);
Copy the code

Assuming that the User above is read from the database, we null and compare.

public class EqualsDemo {
    public static void main(String[] args) {
       User user = new User();
       user.setId(1);
       if(user! =null){
           System.out.println(user.getName().equals("aa")); }}}Copy the code

Obviously, the above program will raise a null pointer exception, but we may not notice the problem while writing.

But if we write it the other way around,

System.out.println("a".equals(user.getName()));
Copy the code

Then the ending would have been different. So pay attention, pay attention to every little detail of the programming process, and your program will be more robust.

The last

One can plant trees, and another can enjoy the shade. One step on the pit, the next generation continue to fall, hahaha.

Come all come, click “like” and then go!

Follow WangScaler and wish you a promotion, a raise and no bucket!