Soul torture: do you really understand the system out. Does println () work?

Original / Zhu Jiqian

Soul torture, this Duxiu classmate, can you solve this problem?

Please talk about the principle of "system. Out. Println()"

Before going deeper, ask yourself a few questions:

What is system? What is out? What is println? Why can the three codes realize the function of printing information?

Next, we will take questions to get familiar with our old man who has been together for a long time.

Start with system and explore step by step.

On Baidu Encyclopedia, there is a description of system: the system class represents the system, in which many system level attributes and control methods are placed inside the class.

In short, this class is related to the system and can obtain many attributes and methods inside the system. Some of its source codes are as follows:

Open the source code and find that this is a final defined class. Secondly, the constructor of this class is defined with private permission. According to these two situations, this class can neither be inherited nor instantiated into objects. At the same time, it should be noted that many variables and methods defined in this class are defined by static, that is, these class members belong to classes rather than objects.

Therefore, if you need to call these static defined properties or methods in the class, you can call them directly through "class name. Member name" without creating an object.

In the system source code, it should be noted that in, out and or represent standard input stream, standard output stream and standard error output stream respectively.

At this point, you can gradually see the system out. The shadow in println, yes, the system in this line of code Out refers to the static member out in the reference system class. It is a reference variable of printstream type, called "byte output stream". As an out reference variable defined by static, it is initialized when the class is loaded. After initialization, a printstream object will be created to assign a value to out, and then the methods defined in the printstream class can be called.

How to create a printstream and assign it to the static member out is explained later in this article.

Next, enter the printstream class——

It is found that there are many overloaded methods named println in this printstream.

This is the last question we need to answer in this article, that is, what is println?

It is actually a method in the printstream printout stream class.

In every println method with reference, the last call is print () and newLine ().

It is worth noting that these println methods with parameters are modified by synchronizing synchronized, which shows that system out. Println is actually thread safe. At the same time, it should be noted that in the case of multithreading, when a large number of methods execute the same println printing, their synchronized synchronization performance and efficiency may have serious performance problems. Therefore, in actual production, log. Is generally used Info() prints the log in a similar way without using system out. println。

In the above code, newline () means to print line breaks.

As we all know, with system out. When println() is used to print information, each printed information will wrap. The reason for the line break is that println() is implemented internally through the newline() method.

If replaced with system out. Print(), there will be no newline.

Why doesn't print () wrap?

By analyzing the code in print (), we can know that it is because the newline () method is not called in its method to realize line feed——

@H_ 404_ 527@ these overloaded methods call the same write () method. It is worth noting that when calling write (), some methods convert the parameters to string type, and then enter the details of the write () method——

Among them, the method of ensureopen() is to judge whether the out stream has been opened. The detailed method is as follows:

@H_ 404_ 527@ it can be seen from the method that when writing print information, it is necessary to judge whether the printstream stream has been turned on. If not, the print information cannot be written to the computer, so an exception prompt indicating that the stream is closed is thrown: "stream closed"

If the flow is on, you can execute TextOut write(s);

According to my personal understanding, TextOut here is the bufferedwriter reference variable, that is, the stream written in the IO stream, which will eventually write the information to the console, that is, console printing. It can be understood that the console is a file, but we can see what is in it in real time. In this way, every time we write something, it will be presented in the file in real time, that is, the console print information that we can see.

So, the question is, which line of code is written to the console file? How do system, out and println work together?

Let's go back to the beginning of the system class——

The above static code will be initialized in the initialization phase of the class, which will call a native method registernatives(). According to the English Annotation "VM will invoke the initializesystemclass method to complete" of this method, VM will call the initializesystemclass method to complete this class initialization.

We found the initializesystemclass method. Only the core code required in this article is listed below, with some comments:

Focus on these two lines of code:

@H_ 404_ 527 @ I Here, we will analyze it line by line. Firstly, filedescriptor is a "file descriptor", which can be regarded as a file. It has the following three attributes:

FileDescriptor. Out stands for "standard output (screen)", which can be easily interpreted as a file of standard output to the console, that is, the console.

New fileoutputstream (filedescriptor. Out) this line of code indicates that the information is output to the screen console through the file output stream.

If you still don't understand, give a common example——

This is a relatively simple way to write a file through the fileoutputstream output stream. The path "C: \ file. TXT" here is the same as filedescriptor. TXT The out method is similar. It describes a file that can write data, except filedescriptor Out is special. It describes the screen, which is often called the console.

II Next is newprintstream (fdout, props. Getproperty ("sun. Stdout. Encoding"))——

This method is to create a bufferedoutputstream buffer output stream for the output stream, which plays the role of stream buffer, and finally create a print output stream through new printstream().

Print output can be realized through the print interface of the stream, such as print(), println().

III Finally, execute setout0 (newprintstream (fdout, props. Getproperty ("sun. Stdout. Encoding"));

It can be seen that this method is a native method. Children's shoes of interest can continue to be studied in depth. Here, the generated printstream object is assigned to the static object reference variable: out in the system.

Here, we return to the place where we started: system out. Println, yes, the out in this is the assignment of printstream object through setout0. Since we can get the object reference out of printstream, we can naturally access any public method in printstream class, including println(), including print(), and so on.

You can extract the above initialization out source code and redo a manually printed test, such as:

After execution, it is found that the word "test print" can be printed on the console.

Finally, to summarize, system out. The principle of println is that when the class loads the system, it will initialize the initializesystemclass () method of the system. In this method, a printstream object will be created. Then, through the setout0 (printstream out) method, the initialized printstream object will be assigned a value to the system static reference variable out. After the out object address is assigned, you can call various public modified methods in printstream, including println(), print() and other methods for printing information through out Println ("XXXX") prints "XXXX" to the console, which is equivalent to system out. println("xxxx")。

The above is system out. The execution principle of println.

If there is any deficiency, please also point out the correction.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>