Description of Object (1): toString, objecttostring
An Object exists as a detachment in Java, and defines methods that all objects share.
Features:
1. the java. lang package does not need to be displayed for import during use. The Compiler automatically imports the package during compilation.
2. The Object class is the root of the class hierarchy. All classes in Java basically inherit from this class.
3. The Object class is the only class in Java without a parent class. All other classes, including standard container classes, such as arrays, inherit the methods in the Object class.
The following describes in detail some methods of Object.
The toString method returns a string type description about this class. We usually rewrite it to the description of the object we need, such as the attributes of the object.
In some cases, although the toString method is not displayed, the program still calls the toString method, for example, as Systrm. out. println () parameter; when it is connected to the String through the + operation.
Source code:
Public String toString () {return getClass (). getName () + "@" + Integer. toHexString (hashCode ());}
We can see that by default, the type of the object is returned + @ + its hash value.
Note: The returned information after rewriting should be a concise, clear, and easy-to-read description of the instance.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.