Objects is a new tool class in Java 7 that provides tools to manipulate objects, most of which are "null pointers" safe.
Objects implements the following methods:
(1) How the Hascode (Object) method is implemented in the source code:
public static int hashcode (Object o) { return o! = null? O.hashcode (): 0; }
originally in the Hascode (object) method to make a layer of judgment, if the object is null, it returns an int 0 to it, instead of this object
If it is not NULL, it gets its hash value, which means that when we use this method, we don't have to worry about whether the object is empty.
(2) How the hash (Object ... values) method is implemented in the source code:
public static int hash (Object ... values) { return Arrays.hashcode (values); }
As you can see in the above program, the parameters passed in are mutable because the object is used to decorate and pass ... To implement an incoming parameter can be multiple
Parameters of the same type.
Then take a look at how Arrays.hashcode is implemented:
public static int hashcode (Object a[]) { if (a = = null) return 0; int result = 1; for (Object element:a) result = * result + (element = = null? 0:element.hashcode ()); return result; }
It turns out that the hash value of each incoming element is obtained through a foreach loop, and the hash value of each element is passed result = * * result + ( element
= = null? 0:element.hashcode ()); To add and finally get the value after each hash value is added.
(3) How the ToString (Object O) method is implemented in the source code:
public static String toString (Object o) { return string.valueof (o); }
about the way string.valueof (Object O) is implemented in the source code:
public static String valueOf (Object obj) { return (obj = = null)? "Null": Obj.tostring (); }
still the same, judge whether the object is empty, if NULL to return a "null" string, conversely, get the information of this object and return.
(4) How the ToString (Object o, String Nulldefault) method is implemented in the source code:
public static string toString (Object o, string nulldefault) { return (o! = null)? O.tostring (): Nulldefault; }
as with the toString (object o) method above, get the information of the object, except that if the object is empty, the custom Nulldefault is returned .
String.
Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/43927021 Emotional Control _
Java Learning Note 19