Java toString () method
(i), convenient println () The output of the method
Public clasststring {PrivateString name; Publictstring (String name) { This. Name =name; } PublicString toString () {return"Name:" +name; } Public Static voidMain (string[] args) {//TODO auto-generated Method StubTstring student=NewTstring ("Gary"); System.out.println (Student); }}
Output:
Name: Gary
ToString is a method that is already in object inheritance, and all classes inherit object, so "all objects have this method"
It is usually just for the convenience of the output, such as System.out.println (XX), the parentheses inside the "XX" if it is not a string type, automatically call XX's ToString () method
The (ii), ToString () method is used to return the value of a number object expressed as a string.
Public class Text { publicstaticvoid main (string[] args) { // TODO auto-generated Method stub Integer x = 5; System.out.println (X.tostring ()); System.out.println (integer.tostring);} }
Output:
512
If the method uses the native data type as a parameter, the String object value of the native data type is returned.
If the method has two parameters, returns the string representation of the first argument that is specified by the second argument as a cardinality.
return value
Java Foundation __tostring () method