Introduction to Object class and ToString method

Source: Internet
Author: User

I. Introduction of object class

The object class in Java is a special class, Java only supports single inheritance, subclasses can only inherit from a parent class, if the parent class is inherited from another parent class, then he can only have a parent class, the parent class has a parent class, there can only be one, Java to organize this kind of organization is more convenient , it provides a root class, which is equivalent to all classes inheriting from this class, which is called object. So the object class is the root class of all Java classes and is the ancestor of all Java classes. All classes, no matter who they are, inherit from it.

Ii. Introduction to tostring Method

when a string is connected to another type, the other type is automatically converted to a string type and then concatenated with the string. the underlying data type int,float,double converted to strings is simpler, converted by their numbers, can be reference types, person p = new person (); A string plus this p, You don't know how to convert this p to a string, because this p is a reference type. p = new person ();

public class Testtostring {

public static void Main (string[] args) {
Dog d = new Dog ();
* If the ToString method is not overridden, the output default string content is "class name + hash code",
* such as:[email protected]
*The D here is a reference type, and when printed, this reference type D automatically calls the ToString () method to convert itself to a string and then to the string "d:=" .
* then be printed together. D why the ToString () method can be called automatically, and the dog does not declare the ToString () method.
* This is because the ToString () method is the method inside the object class, and all classes inherit from the object class,
* The dog class is certainly no exception, so the dog inherits the ToString () method inside the object class,
* So the object of the dog class can of course call the ToString () method directly.
* But the dog class is not satisfied with the inherited ToString () method,
* Because using this continuation of the ToString () method to convert a reference object to a string output is a series of confusing hash encodings.
* In order to make the printed information so that normal people can understand, so in the dog class, the inherited ToString () method to rewrite,
* Making it possible to call this ToString () method to convert a reference object into a string when printed out is some normal, readable information.
* When a subclass overrides a method inherited from a parent class, copy the declaration of the method to be overridden from the parent class directly into the subclass,
* This will not be an error when overridden in a subclass.
System.out.println ("dog=" +d);//Print Result: Dog=i ' m a cool dog

Class dog{
* After you rewrite the ToString () method inside the object class,
* The overridden ToString () method is called when the Reference object is called automatically.
* The printed display information is the string information we want to return when we rewrite the ToString () method.
* No longer a hash code that you can't read.
Public String toString () {
Return "I ' m a Dog";

Any class inherits from the object class, so you can override the ToString () method in any class. the ToString () method is used when a reference object is connected to a string, or when the reference object is printed directly, and the reference object automatically calls the ToString () method, which returns a string that represents the normal information of the referencing object. The contents of this string are defined by ourselves, and the default string content is "class name + hash code ". So we can change the default string content to the string content of the normal information we want to express by rewriting the ToString () method inside the class.

Introduction to Object class and ToString method

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.