One: the value is null

  • A. quals(b), where a is null, throws a NullPointException.

  • A. quals(b), a not null, b is null, return false

  • Objects.equals(a, b) returns true if both a and B are null, false if one of a and B is not null. Note: no null pointer exception is thrown.

    Null. Equals (” ABC “) → Raise NullPointerException “ABC “. Equals (null) → Return false null. Equals (null) → Raise NullPointerException NullPointerException

    Objects.equals(null, “ABC “) → Return false objects.equals (null, null) → Return true

Two: the value is an empty string

  • Return true if a and B are both null strings: “”; return false if either a or B is not a null string;

  • Objects.equals in this case behaves the same as in case 1.

    “ABC”. The equals (” “), return false. “” equals (” ABC”) – > return false “” equals (” “) to return true

    Objects.equals(” ABC “,””) → False objects.equals (“”,””) → False Objects.equals(“”,””) → True

Three: source code analysis 1.

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 a 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)); }... }Copy the code

2. Note First, the IP address of the object is determined. If the IP address is true, no further determination is performed. If a is not null, then a is not null. If a is not null, then a is not null. So, if both are null, it’s true on the first judgment. If it is not null, the address is different, and it is important to judge a. quals(b).