Our first Java program is "Hello World"
1 public class HelloWorld {
2 public static void Main (string[] args) {
3 System.out.println ("Hello World");
4}
5}
How exactly does the above program output "Hello World" on the screen? This is what is meant to be explained, namely the SYSTEM.OUT.PRINTLN ("Hello World") principle.
Let's look at the SYSTEM.OUT.PRINTLN process first. First look at the definition of System.java, the source code is as follows:
Public final class System {
...
Public final static printstream out = null;
...
}
From that, we find that
Out is a static variable of System.java.
(02) and Out is the PrintStream object, there are many overloaded println () methods in Printstream.java.
OK, we know that out is a PrintStream object. Next, see how it is initialized, how does it relate to the screen output?
We still step into the analysis, first look at the System.java Initializesystemclass () method.
1. The source code of Initializesystemclass () is as follows: Mark out part as Red
private static void Initializesystemclass () {props = new Properties (); InitProperties (props);
Initialized by the VM sun.misc.VM.saveAndRemoveProperties (props);
LineSeparator = Props.getproperty ("Line.separator");
Sun.misc.Version.init ();
FileInputStream Fdin = new FileInputStream (filedescriptor.in);
FileOutputStream fdout = new FileOutputStream (filedescriptor.out);
FileOutputStream fderr = new FileOutputStream (FILEDESCRIPTOR.ERR);
SetIn0 (New Bufferedinputstream (Fdin));
SetOut0 (New PrintStream (New Bufferedoutputstream (Fdout, 128), true);
SetErr0 (New PrintStream (New Bufferedoutputstream (Fderr, 128), true);
LoadLibrary ("Zip");
Terminator.setup ();
Sun.misc.VM.initializeOSEnvironment ();
Thread current = Thread.CurrentThread ();
Current.getthreadgroup (). Add (current);
Setjavalangaccess ();
Sun.misc.VM.booted (); }
We just need to focus on the Red Code section above:
FileOutputStream fdout = new FileOutputStream (filedescriptor.out);
SetOut0 (New PrintStream (New Bufferedoutputstream (Fdout, 128), true);
Subdividing These two sentences can be divided into the following steps:
1th step filedescriptor fd = filedescriptor.out;
2nd Step FileOutputStream fdout = new FileOutputStream (FD);
3rd Step Bufferedoutputstream bufout = new Bufferedoutputstream (fdout, 128);
4th Step PrintStream PS = new PrintStream (Bufout, true);
5th Step SetOut0 (PS);
Description
(01) Step 1th, to get the static member Out,out in Filedescriptor.java is a FileDescriptor object, which is actually an identifier for the standard output (screen). For a detailed description of FileDescriptor, you can refer to the Bowen "Java IO series 09 FileDescriptor Summary".
The code in Filedescriptor.java related to Filedescriptor.out is as follows:
Public final class FileDescriptor {
private int fd;
public static final FileDescriptor out = new FileDescriptor (1);
Private FileDescriptor (int fd) {
this.fd = FD;
Usecount = new Atomicinteger ();
}
...
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/