Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Don't use a.equals (b) to judge why objects are equal.

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "do not use a.equals (b) to judge why the object is equal". In daily operation, I believe many people have doubts about what the reason for object equality is without a.equals (b). The editor consulted all kinds of data and sorted out a simple and useful method of operation, hoping to help you answer the question of "do not use a.equals (b) to judge why the object is equal." Next, please follow the editor to study!

One: if the value is null

A.equals (b), an is null, and throws a NullPointException exception.

A.equals (b), an is not null, b is null, and returns false

When comparing Objects.equals (a, b), true is returned if both an and b are null, and false is returned if one of an and b is null and the other is not null. Note: null pointer exceptions are not thrown.

Null.equals ("abc") → throws NullPointerException exception "abc" .equals (null) → returns falsenull.equals (null) → throws NullPointerException exception Objects.equals (null, "abc") → returns falseObjects.equals ("abc", null) → returns falseObjects.equals (null, null) → returns true

Two: if the value is an empty string

If both an and b are null strings: "", a.equals (b) returns true, and returns false if one of an and b is not a null string

In this case, Objects.equals behaves in line with case 1.

"abc" .equals ("") → returns false "" .equals ("abc") → returns false "" .equals ("") → returns trueObjects.equals ("abc", "") → returns falseObjects.equals ("", "abc") → returns falseObjects.equals ("", ") → returns true

Three: source code analysis

1. Source code.

Public final class Objects {private Objects () {throw new AssertionError ("No java.util.Objects instances for you!");} / * * Returns {@ code true} if the arguments are equal to each other * and {@ code false} otherwise. * Consequently, if both arguments are {@ code null}, {@ code true} * is returned and if exactly one argument is {@ code null}, {@ code * false} is returned. Otherwise, equality is determined by using * the {@ link Object#equals equals} method of the first * argument. * * @ param an an object * @ param b an object to be compared with {@ code a} for equality * @ return {@ code true} if the arguments are equal to each other * and {@ code false} otherwise * @ see Object#equals (Object) * / public static boolean equals (Object a, Object b) {return (a = = b) | (a! = null & & a.equals (b));}.}

two。 Description

First of all, the object address is judged, and if it is true, the judgment will not continue.

If it is not equal, the latter expression means that it is determined that an is not empty, and then based on the knowledge points above, there will be no more null pointers.

So, if it's all null, it's true in the first judgment. If it is not empty and the address is different, it is important to determine a.equals (b).

At this point, the study of "do not use a.equals (b) to judge why objects are equal" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report