The ToString () method in Java

Source: Internet
Author: User

The ToString () method in Java

Directory

The ToString () method in Java

1.???? The ToString method of the object

2.???? The ToString method of the base type

3.???? The ToString method of the array

4.???? Reference documents

?

?

  1. The ToString method of the object

    in the JDK API in the Object object that toString The method is described as follows:

    toString :

    Public String toString ()

    Returns the string representation of the object. Typically, tostring method returns a " in text mode " It is recommended that all subclasses override this method.

    Object Class of toString method returns a string that consists of the class name (the object is an instance of the class), at Tag Character "@" and an unsigned hexadecimal representation of the hash code for this object. In other words, the method returns a string whose value is equal to:

    GetClass (). GetName () + ' @ ' + integer.tohexstring (hashcode ())

    Return:

    The string representation of the object.

    ?

    Conclusion:

    ???? if the ToString method is overridden in the object , the object's own tostring method is called, otherwise the the toString method.

    ?

  2. The ToString method of the base type

    Basic types of toString method is called by the Java.io.PrintStream of the println (int) method, the key code is as follows:

    Write (string.valueof (i));

    ?

    can be done by JDK self-bringing tools JAVAP Anti-compile code to view.

    find the generated . Class the directory where the file is located, execute the command:

    Javap–c class name

    ?

  3. The ToString method of the array

    For arrays:

    Code:

    int[] arr = new int[] {1, 2, 3};

    System.out.println (arr);

    Output content:

    [[Email protected]

    ?

    Conclusion:

    An array is also an object, and an array object is not instantiated from a class, but by the JVM created directly, This is called the toString method of the java.lang.Object .

    ?

    ?

    ?

  4. Reference documents

    JDK API

    JAVA is the array in the object?

The ToString () method in Java

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.