Covers a lot of Java OOP technology
This is not because there is a great demand for the use of this statement in real-world problems. (In fact, in a GUI-driven software product world, there is probably very little demand for the use of this statement.) Rather, it is because a lot of Java object-oriented technology is embodied in this single statement.
In that scenario, I would expect to receive a verbal dissertation of fifteen to twenty minutes in length to cover all the important points.
The short version
Let me give you the short version. There is a class named System. The System class declares three static (class) variables having the following types, names, and modifiers:
- public static final PrintStream out
- public static final InputStream in
- public static final PrintStream err
(Note that these class variables are also declared final, causing them to behave as constants.)Access the out variable without an object
Because out is a class variable, System.out returns the contents of the class variable named out (an object of the System class is not required in order to access a class variable of the System class).
In general, (ignoring the possibility of subclasses and interfaces) because out is a reference variable of type PrintStream, the returned value must either be null (no object reference) or a reference to a valid PrintStream object.
Object of the PrintStream class
When the Java Virtual Machine starts an application running, it instantiates an object of the PrintStream class and connects it to the standard output device.
(By default, the standard output device is typically the computer screen, but it can be redirected at the operating system level to be some other device. The following discussion assumes that the screen is the standard output device.)Assign object's reference to out variable
When the PrintStream object is instantiated by the virtual machine, the object's reference is assigned to the class variable of the System class named out.
(Because the variable named out is final, the contents of the variable cannot be modified later.)Reference to a PrintStream object
Therefore, the expression System.out returns a reference to the PrintStream object, which is connected to the standard output device.
Many instance methods
An object of the PrintStream class contains many instance methods. This includes numerous overloaded versions of a method named println. The signature of one of those overloaded versions of the println method follows:
public void println(Object x)
Textual representation of an object
The purpose of this overloaded version of the println method is to:
- Create a textual representation of the object referred to by the incoming parameter of type Object (because Object is a totally generic type, this version of the println method can accept an incoming parameter that is a reference to any type of object)
- Send that textual representation to the output device
In general ...Our old friend, the toString method
(In general, a new PrintStream object can be connected to a variety of output devices when it is instantiated. However, in the special case of the PrintStream object instantiated by the virtual machine when the program starts, whose reference is stored in the class variable named out of the System class, the purpose of the object is to provide a display path to the standard output device.)
To accomplish this, the code in the println method invokes the toString method on the incoming reference.
(I discussed the toString method in detail in earlier lessons in this miniseries.)The toString method may, or may not, have been overridden in the definition of the class from which the object was instantiated, or in some superclass of the class from which the object was instantiated.
Default version of toString
If not overridden, the default version of the toString method defined in the Object class is used to produce a textual representation of the object. As we learned in an earlier lesson, that textual representation looks something like the following:
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




