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