The ToString method in Java

Source: Internet
Author: User

1. The object class is defined with the public String toString () method whose return value is a String type that describes the information about the current object;

2. The toString () method of the object class is automatically invoked when a String is connected to other types of data, such as SYSTEM.OUT.PRINTLN ("info" +person);

3. You can override the ToString () method on a user-defined type as needed, such as:

Class Dog {public String toString () {
Return "I ' m a good dog!!!";    }}class Test {public static void main (string[] args) {Dog d = new Dog ();  System.out.println ("D: =" +d); }}//operation Result: D: = I ' m a good dog!!!

If the ToString () method is not overridden in the Dog class, it is:

Class Dog {//Public String toString () {//return "I ' m a good Dog!!!"; /}}class Test {public static void main (string[] args) {Dog d = new Dog ();  System.out.println ("D: =" +d); }}//operation result is: D: = [email protected]

The return result is: class name [email protected]+ hash code

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.