System. Out. println (Object OBJ) implementation process

Source: Internet
Author: User

I am afraid the most common statements used in Java are system. out. println (); this sentence is often used to simulate many things. How is this sentence implemented?

First, system is a core class in the Java. lang package. If you check its definition, you will find a line like this:

Public static final printstream out;

Next, click the printstream hyperlink. On the Method page, you will see a large number of defined methods. Look for println, and there will be a line like this:

Public void println (Object O)

The object is not a string. What exactly does it print? Let's look at the following sentence:

Prints an object. The string produced byString.valueOf(Object)Method is translated into bytes according to the platform's
Default character encoding, and these bytes are written in exactly the manner ofwrite(int)Method.

It means that it will call String. valueof (object) to obtain the string form of the object. So how does string. valueof (object) process the object? The JDK help document explains the following:

If the argument isnull, Then a string equal
"null"; Otherwise, the valueobj.toString()Is returned.

That is, if it is not null, it calls the tostring () method of the object.

Well, now you should understand why we need to call it like that. Out is a static variable of system, so we can use it directly, and the class to which out belongs has a println method.
The final string to be printed is defined by the tostring method in the class.

Appendix: inheritance relationship of the printstream class

Class printstream

  • Java. Lang. Object
    • Java. Io. outputstream
      • Java. Io. filteroutputstream
        • Java. Io. printstream

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: info-contact@alibabacloud.com 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.