ToString Method overrides in Java Foundation

Source: Internet
Author: User

ToString (Public StringtoString())
returns the string representation of the object. Typically, the toString method returns a string that represents this object as text. The result should be a concise but easy-to-read information expression. It is recommended that all subclasses override this method.

ObjectThe method of the class toString returns a string consisting of the class name (the object is an instance of the class), the at marker " @ ", 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:


   

return:

the string representation of the object.
As
an example:
Package toString; Public classTostringtest extends object{Private intID; PrivateString name;  PublicTostringtest (intid,string name) {         This. id=ID;  This. name=name; } //automatically generated by shortcut //Public String toString () {//return "[id=" + ID + ", name=" + name + "]";//    }        //The rewrite of toString     PublicString toString () {return "ID:"+ ID +","+"Name:"+name; }} 
Package toString;  Public class Tostringmain {    publicstaticvoid  main (string[] args) {        //  All reference types of the parent class are object        tostringtest t=new tostringtest (123," Dong Shihao " );        System.  out . println (t);    }}

The result:id:123,name: Dong Shihao

The ToString method does not rewrite when the result of the output is:[email protected]

ToString Method overrides in Java Foundation

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.