Description of Object (1): toString, objecttostring

Source: Internet
Author: User

Description of Object (1): toString, objecttostring

An Object exists as a detachment in Java, and defines methods that all objects share.
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.

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: 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.