Java Learning Note 15

Source: Internet
Author: User

The object class is the parent class of all classes, arrays, and enumeration classes, and is the root class of the class hierarchy. Each class uses object as a superclass. All objects, including


arrays) Implement this class of methods.


The object class implements the following methods:


Let's see how the ToString () method is defined in the source code:

Public String toString () {        return getclass (). GetName () + "@" + integer.tohexstring (Hashcode ());    }


The following procedure shows that the ToString () method returns the full-qualified name of the class plus the "@" symbol, and finally the hash value of the object, and


The hash value is converted to a hexadecimal output.


In peacetime, we can get the string of the object information through the ToString () method, for example:


Class User {    private String name;    private int age;        Public User (String Name,int age) {    this.name=name;    this.age=age;    }        Protected String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }} public class Main {public static void main (string[] args) {User user=new User ("Bill", 25); System.out.println (User.tostring ());}}

Output:

[Email protected]


In addition to using the ToString () method directly, we can overload this method, for example:


Class User {    private String name;    private int age;        Public User (String Name,int age) {    this.name=name;    this.age=age;    }        Protected String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }        @Override public    String toString () {    return "name=" +name+ "\nage=" +age;    }} public class Main {public static void main (string[] args) {User user=new User ("Bill", 25); System.out.println (User.tostring ());}}

Output:

Name=bill
Age=25






Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/43909597 Emotional Control _  




Java Learning notes

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.