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
-