Java Learning Note 19

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.