Objects and Object differences
Object is the base class for all classes in Java and is located in the Java.lang package.
Objects is a tool class for Object, located in the Java.util package. It only appears from jdk1.7, which is not inherited by the final modifier, and has a private constructor.
It consists of a number of static and practical methods, either null-save (null pointer-safe) or null-tolerant (tolerant of null pointers), used to calculate the hashcode of an object, the string representation of the returned object, and a comparison of two objects.
Objects Each method introduction and analysis equals
The Equals method is to determine whether two objects are equal.
The Object.Equals method easily throws a null pointer exception when comparing two objects.
When I first came to work, an old employee taught me "when a string constant is compared to a variable object, the constants are written outside of equals, and the variables are placed inside the equals () brackets. "That's the reason.
If the two variables are compared, it is necessary to add a non-null judgment.
* return (this = = obj) is called within the Object.Equals method. The Equals method is redefined in the string class based on whether the contents of the strings are equal.
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/831511/201702/831511-20170210173921026-1612060000. PNG "width=" 567 "height=" 195 "style=" margin:0px;padding:0px;border:0px; "/>
Now, the Objects.equals method has made a non-null judgment, so no null pointer exception is thrown, it is null-save null pointer security, and it can simplify the code.
The source code is as follows:
public static Boolean Equals (object A, object B) {return (a = = b) | | (A! = null && a.equals (b)); }
Back to Catalog
Deepequals
As the name implies, a depth comparison of two objects.
When a parameter is an array object, its method is used internally by the algorithm of the Arrays.deepequals0 method.
The advantage of using the Objects.deepequals method is that when we write business code, we can use this method directly to judge two complex types,
For example, a generic list object list<t>, or objects that are reflected by reflection, are not clear about the specific type of the object.
The source code is as follows:
650) this.width= 650, "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
public static boolean deepequals (object a, object b) { if (a == b) return true; else if (a == null | | b == null) return false; else return arrays.deepequals0 (a, b); }
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
A short description of the Arrays.deepequals0 method:
If the argument is an array of type object, the Arrays.deepequals method is called, and in the loop of the parameter array, the DEEPEQUALS0 is called recursively until a different element appears . or the end of the cycle;
If the argument is an array of the base type, the Arrays.equals method is called based on that type. The Arrays tool class overloads the Equals method according to eight basic types.
Back to Catalog
Hashcode
Returns an integer value that represents the hash code value of the object. If the parameter object is empty, the integer 0 is returned, and if not NULL, the Object.hashcode method is called directly.
The source code is as follows:
public static int hashcode (Object o) {return o! = null? O.hashcode (): 0; }
Object supports the Hashcode method to improve the performance of a hash table (such as a hash table provided by java.util.Hashtable).
Taking set set as an example, when adding an object, you need to determine if an existing collection already has an object equal to this object, and if there is no Hashcode () method, you need to traverse the set one at a time and use the Equals () method to determine whether two objects are equal, and this algorithm has an O ( N). By means of the Hascode method, the hash code of the object to be newly added is computed, and then the position of the object is computed based on the hash algorithm, and the object can be judged directly at this position.
(Note: The bottom of set is implemented by the principle of map)
Back to Catalog
Hash
Generates a hash code for a series of input values, and the parameter of the method is a mutable parameter.
The source code is as follows:
public static int hash (Object ... values) {return Arrays.hashcode (values); }
It is to put all the input values into an array, and then call the Arrays.hashcode (object[]) method to implement the hash code generation.
The hash method is useful when overriding the Object.hashcode method when an object contains more than one member.
Give an example of Java source code:
The Java.lang.invoke.MemberName class, which has class<?> Clazz, String name, object type, int flags, object resoulution, and several member variables,
The Hashcode method for this class is as follows:
@Override public int hashcode () {return Objects.hash (Clazz, Getreferencekind (), Name, GetType ()); }
Warning: When the supplied argument is a reference to an object, the return value is not equal to the hash code of the object reference. This value can be computed by calling the Hashcode method.
Back to Catalog
TostringtoString (Object o)
Returns a string representation of the specified object. If the argument is null, the string "null" is returned.
The method internally calls the
return String. valueOf (o);
The internal implementation of the string.valueof (Object obj) method is
return (obj==NULL) ?"NULL": obj. toString ();
The internal implementation of the Object.ToString () method is
return getclass (). GetName () + "@" + Integer. tohexstring (Hashcode ());
toString (Object o, String nulldefault)
Returns a string representation of the specified object. If the argument is null, the object specified by the second parameter, Nulldefault, is returned.
Back to Catalog
Compare
Returns an integer of 0 if the two parameters are the same. Therefore, if all two parameters are null, the integer 0 is returned as an empty object.
Note: If one of the arguments is NULL, a null pointer exception is thrown nullpointerexception depends on the sort policy, and if so, the null value is determined by comparator.
The source code is as follows:
public static <T> int compare (t A, t B, comparator<? super T> C) {return (a = = B)? 0:c.compare (A, b); }
Back to Catalog
Requirenonnull
Requirenonnull (T obj)
Checks that an object reference of the specified type is not NULL. Throws a null pointer exception when the argument is null. The main purpose of this method is to verify the parameters in the method and the constructor function.
The source code is as follows:
public static <T> t Requirenonnull (t obj) {if (obj = = null) throw new NullPointerException (); return obj; }
As an example:
When we create an object by using the constructor with the parameter, the object is created and the parameter is verified. It also simplifies a lot of code.
public class Foo {public Foo (bar bar) {This.bar = Objects.requirenonnull (bar); } }
requirenonnull (T obj, String message)
This method is an overloaded method of Requirenonnull, and throws a custom exception message according to the second parameter message when the parameter being validated is null.
The source code is as follows:
public static <T> t Requirenonnull (t obj, String message) {if (obj = = null) throw new Nullpointer Exception (message); return obj; }
requirenonnull (T obj, supplier<string> messagesupplier)
Checks that the specified object reference is not NULL, or throws a custom null pointer exception if it is empty. Starting from jdk1.8.
Unlike the Requirenonnull (Object, String) method, this method allows the creation of a message to be deferred until after the null check has ended.
While this may be a performance advantage in a non-empty example, you should be careful when you call this method, and the cost of creating a message supplier is less than creating a string message directly.
The source code is as follows:
public static <T> t Requirenonnull (t obj, supplier<string> messagesupplier) {if (obj = = null) throw New NullPointerException (Messagesupplier.get ()); return obj; }
Back to Catalog
IsNull
An empty method that returns true if the argument is null. Starting from jdk1.8.
The source code is as follows:
public static Boolean isNull (Object obj) {return obj = = null; }
Apinote: The method exists for the Java.util.function.Predicate class, filter (Objects::isnull).
Take a look at the code in the predicate class that uses this method:
Static <T> predicate<t> isequal (Object targetref) {return (null = = Targetref)? Objects::isnull//Double colon, which represents the method reference. : Object---Targetref.equals (object); Here is a lambda expression. The object object is received, and the result of the comparison of the parameter Targetref with the objects is returned. }
Nonnull
Determines the non-null method, which returns true if the argument is not empty. Starting from jdk1.8.
The source code is as follows:
public static Boolean nonnull (Object obj) {return obj! = null; }
Apinote: The method exists for the Java.util.function.Predicate class, filter (Objects::nonnull).
Back to Catalog
Java.util.Objects Source Learning