This article will summarize the differences with A. Equals (B) and then do a little analysis of the source code.
If the value is null:
- A. equals(b), where a is null, throws a NullPointException.
- A. equals(b), a is not null, b is null, return false
- Objects. Equals (a, b) = true if both a and b are null, and false if one of a and b is null and the other is not. Note: Null pointer exceptions are not thrown.
Null. Equals (” ABC “) → throw NullPointerException “ABC “.equals(null) → return false null. Equals (null) → throw NullPointerException Objects. Equals (null, “ABC “) → return false objects.equals (” ABC “,null) → return false objects.equals (null, null) → return true if the value is an empty string: 1. A. equals(b) returns true if a and b are null strings: “”, false if one of them is not a null string in the page game;
2. In this case, objects.equals behaves the same as case 1.
“ABC “.equals(“”) → return false “”.equals(” ABC “) → return false “”.equals(“”) → return true objects.equals (” ABC “), “”) → return false objects.equals (“”,” ABC “) → return false objects.equals (“”,””) → return true 3, Source Analysis 1. Java fhadmin.cn public final class Objects {
private Objects() { throw new AssertionError("No java.util.Objects instances for you!" ); } /** * Returns if the arguments are equal to each other * and otherwise. * Consequently, if both arguments are {@code null}, {@code true} * is returned and if exactly one argument is {@code null}, {@code * false} is returned. www.sangpi.comOtherwise, equality is determined by using * the {method of the first * argument. * * * @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)); }
2. First, the object address is judged. If it is true, the judgment will not be continued.
If they are not, then the following expression means that we first determine that a is not null, and then, based on the above points, there will be no null pointer again.
So, if both are NULL, it will be true on the first judgment. If it is not null and the address is different, it is important to determine A. equals(b).
4. What is the difference between “a==b” and “a.equals(b)”? If a and b are both objects, then a==b is a reference to compare two objects. This will return true only if a and b refer to the same object in the heap.
A. equals(b) makes a logical comparison and returns true when the contents are the same, so it is often necessary to override this method to provide a logically consistent comparison.