Home  >  Article  >  Java  >  Analysis of pitfall examples of Objects.equals in Java

Analysis of pitfall examples of Objects.equals in Java

WBOY
WBOYforward
2023-05-04 13:28:06744browse

    1. Crime scene

    Suppose there is such a requirement: determine the currently logged in user, and if it is the system administrator we specify, send An email. The system administrator has no special field identification. His user ID is 888. This value is the same in development, testing, and production environments.

    This requirement is really easy to implement:

    UserInfo userInfo = CurrentUser.getUserInfo();
    
    if(Objects.isNull(userInfo)) {
       log.info("请先登录");
       return;
    }
    
    if(Objects.equals(userInfo.getId(),888)) {
       sendEmail(userInfo):
    }

    Get the user information from the context of the currently logged in user, make a judgment, and return directly if the user information is empty.

    If the user information obtained is not empty, then determine whether the user ID is equal to 888.

    • If equal to 888, send the email.

    • If it is not equal to 888, nothing will be done.

    After we logged in with the system administrator account with id=888, we performed relevant operations and prepared to receive the email with great expectations, but found that it was lonely.

    Later, I found that the UserInfo class is defined like this:

    @Data
    public class UserInfo {
        private Long id;
        private String name;
        private Integer age;
        private String address;
    }

    At this time, some friends may say: I don’t see any problem.

    But what I want to say is that there is indeed a problem with this code.

    What's the problem?

    Answer: The member variable id=888 of the UserInfo class is of type Long, while the 888 on the right side of the Objects.equals method is of type int. The two are inconsistent. The result returned is false.

    What kind of reason is this?

    Answer: Dear readers, don’t worry, I will explain it in detail later.

    2. Methods of judging equality

    Let us review together what methods have been used to judge whether two values ​​are equal.

    2.1 Use the == sign

    Before judging whether two values ​​are equal, the fastest way is to use the == sign.

    int a = 1;
    int b = 1;
    byte c = 1;
    Integer d1 = new Integer(1);
    Integer d2 = new Integer(1);
    System.out.println(a == b); 
    //结果:true
    System.out.println(a == c); 
    //结果:true
    System.out.println(a == d1); 
    //结果:true
    System.out.println(d2 == a); 
    //结果:true
    System.out.println(d1 == d2); 
    //结果:false

    I don’t know if you have discovered that the basic types in Java include: int, long, short, byte, char, boolean, float, double. You can use the number to determine whether the values ​​are equal. If a basic type of wrapper class appears, such as Integer, using a basic type and a wrapper class, the use number can be correctly judged and true will be returned.

    When comparing Integer and int, they will be automatically unboxed to determine whether the comparison values ​​are equal.

    But if there are two packaging classes, such as d1 and d2, the result of using the == sign may be false.

    When two Integers are compared, the comparison is whether the references they point to (ie, memory addresses) are equal.

    There is another interesting phenomenon:

    Integer d3 = 1;
    Integer d4 = 1;
    Integer d5 = 128;
    Integer d6 = 128;
    System.out.println(d3 == d4); 
    //结果:true
    System.out.println(d5 == d6); 
    //结果:false

    are all parameters of Integer type, which are directly assigned and compared. The judgment results of d3 and d4 are equal, but the judgment results of d5 and d6 are not equal.

    My friends, are your jaws dropped?

    Answer: Because Integer has a constant pool, the direct Integer data from -128 to 127 is directly cached into the constant pool. So 1 is in the constant pool, but 128 is not.

    However, new Integer objects are not suitable for constant pools. This can be seen from the comparison results of the previous d1 and d2 examples.

    Next, let’s look at the judgment of strings:

    String e = "abc";
    String f = "abc";
    String g = new String("abc");
    String h = new String("abc");
    System.out.println(e == f); 
    //结果:true
    System.out.println(e == g); 
    //结果:false
    System.out.println(g == h); 
    //结果:false

    Ordinary string variables can also return the correct result using the == sign.

    But if an ordinary string variable is used to judge the string object produced by new, false will be returned. This point is different from what I said before about using a basic type and a packaging class, and the result of using number judgment. Strings do not have the function of automatic unboxing, which requires special attention.

    In addition, when the two new string objects are judged using the == sign, false will also be returned.

    2.2 Use the equals method

    Using the == sign above, you can quickly determine whether the eight basic data types are equal. In addition, you can also determine whether the references of two objects are equal. .

    But now there is a problem. It cannot determine whether the specific data values ​​​​of the two objects in the memory are equal. For example:

    String g = new String("abc");
    String h = new String("abc");
    System.out.println(g == h); 
    //结果:false

    String objects g and h are two different objects. When they use the == sign to determine whether the references are equal, they return false.

    So, how do we judge equality when the objects are different but the data values ​​are the same?

    Answer: Use the equals method.

    The equals method is actually a method in the Object class:

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

    This method is very simple and only determines whether the references of two objects are equal.

    Obviously, if the string type directly uses the equals method of the parent class (i.e. Object class) to judge the situation where the objects are different but the values ​​are the same, there is a problem.

    So, the string (i.e. String class) will re-implement the equals method:

    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;
    }

    It will still first determine whether the two object references are equal, and return true if they are equal. Next, the two strings will be compared character by character, and true will be returned only if all characters are equal.

    nice, this can solve the problem of judging g and h:

    String e = "abc";
    String f = "abc";
    System.out.println(e.equals(f)); 
    //结果:true

    It can be seen that we use the equals method rewritten by the String class to judge that the two string objects are different, but When the values ​​are the same, true will be returned.

    3. Null pointer exception

    We already know from the previous point that to determine whether two objects are equal, you can use the == sign or the equals method.

    But if you use them more deeply, you will find a problem, that is: if these two methods determine that the values ​​are equal, a null pointer exception may be reported.

    先看看==号判断的情况:

    int a = 1;
    Integer b = new Integer(1);
    Integer c = null;
    System.out.println(a == b); 
    //结果:true
    System.out.println(a == c); 
    //结果:NullPointerException

    int和Integer使用==号判断是否相等时,Integer会自动拆箱成int。

    但由于c在自动拆箱的过程中,需要给它赋值int的默认值0。而给空对象,赋值0,必然会报空指针异常。

    接下来,看看equals方法:

    String e = null;
    String f = "abc";
    System.out.println(e.equals(f)); 
    //结果:NullPointerException

    由于字符串对象e是空对象,直接调用它的equals方法时,就会报空指针异常。

    那么,如何解决空指针问题呢?

    答:在代码中判空。

    String e = null;
    String f = "abc";
    System.out.println(equals(e, f));

    我们抽取了一个新的equals方法:

    private static boolean equals(String e, String f) {
        if (e == null) {
            return f == null;
        }
        return e.equals(f);
    }

    该方法可以解决空指针问题,但有没有办法封装一下,变得更通用一下,也适用于Integer或者其他类型的对象比较呢?

    答:有办法,继续往下看。

    4. Objects.equals的作用

    Objects类位于java.util包下,它是里面提供了很多对象操作的辅助方法。

    下面我们重点看看它的equals方法:

    public static boolean equals(Object a, Object b) {
        return (a == b) || (a != null && a.equals(b));
    }

    equals方法的判断逻辑如下:

    • 该方法先判断对象a和b的引用是否相等,如果相等则直接返回true。

    • 如果引用不相等,则判断a是否为空,如果a为空则返回false。

    • 如果a不为空,调用对象的equals方法进一步判断值是否相等。

    该方法是如何使用的?

    int a = 1;
    int b = 1;
    Integer c = null;
    
    System.out.println(Objects.equals(a, c)); 
    //结果:false
    System.out.println(Objects.equals(c, a)); 
    //结果:false
    System.out.println(Objects.equals(a, b)); 
    //结果:true

    从上面的列子看出,使用Objects.equals方法比较两个对象是否相等,确实可以避免空指针问题。

    但这个有个疑问:前面使用a==b这种方式比较引用是否相等,当时b为空时,程序直接抛了空指针异常。

    而Objects.equals方法内部也使用了a==b比较引用是否相等,为啥它没有抛异常?

    答:因为而Objects类的equals方法,使用了Object类型接收参数,它的默认值是null,不用进行类型转换,也不用像int类型对象赋值默认值0。

    从上面的理论可以看出,如果我们把代码改成这样,也不会抛异常:

    int a = 1;
    Integer c = null;
    System.out.println(equals(a, c));
    //结果:false

    新定义了一个方法:

    private static boolean equals(Object a, Object b) {
        return a == b;
    }

    执行之后发现,确实没有抛空指针了。

    所以Objects.equals方法再比较两个对象是否相等时,确实是一个不错的方法。

    但它有坑,不信继续往下看。

    5. Objects.equals的坑

    各位小伙们看到这里,可能有点心急了,到底是什么坑?

    废话不多说,直接上例子:

    Integer a = 1;
    long b = 1L;
    System.out.println(Objects.equals(a, b));
    //结果:false

    什么?返回结果是false?

    而如果你直接用==号判断:

    Integer a = 1;
    long b = 1L;
    System.out.println(a == b);
    //结果:true

    返回又是true。

    a和b明明都是1,为什么使用Objects.equals方法判断不相等呢?

    这就要从Integer的equals方法说起来了。

    它的equals方法具体代码如下:

    public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

    先判断参数obj是否是Integer类型,如果不是,则直接返回false。如果是Integer类型,再进一步判断int值是否相等。

    而上面这个例子中b是long类型,所以Integer的equals方法直接返回了false。

    也就是说,如果调用了Integer的equals方法,必须要求入参也是Integer类型,否则该方法会直接返回false。

    原来坑在这里!!!

    其实,如果把代码改成这样:

    Integer a = 1;
    long b = 1L;
    System.out.println(Objects.equals(b, a));
    //结果:false

    执行结果也是false。

    因为Long的equals方法代码,跟之前Integer的类似:

    public boolean equals(Object obj) {
        if (obj instanceof Long) {
            return value == ((Long)obj).longValue();
        }
        return false;
    }

    也是判断入参,如果不是Long类型,则该方法直接返回false。

    除此之外,还有Byte、Short、Double、Float、Boolean和Character也有类似的equals方法判断逻辑。

    由此可见,我们在使用Objects.equals方法,判断两个值是否相等时,一定要保证两个入参的类型要一致。否则即使两个值相同,但其结果仍然会返回false,这是一个大坑。

    那么,如何解决上面的问题呢?

    可以将参数b的类型强制转换成int。

    Integer a = 1;
    long b = 1L;
    System.out.println(Objects.equals(a, (int)b));
    //结果:true

    或者将参数a的类型强制转换成long。

    Integer a = 1;
    long b = 1L;
    System.out.println(Objects.equals(b, (long)a));
    //结果:true

    有些情况也可以直接用==号判断:

    Integer a = 1;
    long b = 1L;
    System.out.println(a==b);
    //结果:true

    除了Objects.equals方法在两个入参类型不同,而会直接返回false之外,java的8种基本类型包装类的equals也会有相同的问题,需要小伙们特别注意。

    之前,如果直接使用java基本类型包装类的equals方法判断相等。

    Integer a = new Integer(1);
    long b = 1L;
    System.out.println(a.equals(b));

    在idea中,如果你将鼠标放在equals方法上,会出现下面的提示:

    Analysis of pitfall examples of Objects.equals in Java

    这时你就知道方法用错了,赶紧修正。但如果直接用包装类的equals方法,有个问题就是可能存在报空指针异常的风险。

    如果你使用Objects.equals方法判断相等,在idea中就并没有错误提示。

    除此之外,我还测试了findBug、sonar等工具,Objects.equals方法两个参数类型不一致的问题,也没有标识出来。

    Guys, take a quick look at your code. Have you found any mistakes?

    Common pitfalls include:

    • Comparison between Long type and Integer type, for example: user ID scenario.

    • Comparison of Byte type and Integer type, for example: status judgment scenario.

    • Comparison between Double type and Integer type, for example: judgment scenario where the amount is 0.

    The above is the detailed content of Analysis of pitfall examples of Objects.equals in Java. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete