简体   繁体   中英

out in System.out.println()

Firstly regrets if this is a very basic question and i promote that I'm still a code monkey. I was asked in an interview to elucidate System.out.println(); I explained the following way

//this class belongs to java.lang package
class System{
  public static PrintStream out;
}
//this class belongs to java.io package
class PrintStream{
 public void println..
}

I've explained that System.out is valid since this is the way we access static variables in java, and out is an object of PrintStream and hence we can access its methods, In sum as

System.out.pritnln(); 

he asked me to simulate a similar kind of program,i traced and it did not work,since System.out is returning null

my question is where is out object instantiated in java ? Is it a predefined object if I'm not wrong. what should be the meticulous explanation for this.

Technically what should we call out? Is out a variable of type PrintStream type or should one say it as an object of type PrintStream ?

System.out is initialized to null when the class is instantiated. This is set by the nullPrintStream() method in System.java , which just returns null .

When the JVM has initialized, it calls the initializeSystemClass() method. This method calls the native method setOut0() which sets the out variable to the appropriate value.

This may seem weird but it is a necessary operation for the following reasons:

  • out cannot be set statically to the value because System needs to be one of the first loaded classes (before PrintStream ).
  • out must be final so that its value cannot be directly overridden by a user.
  • Since out cannot be set statically, and is final, we must override the semantics of the language using a native method, setOut0() .

I hope that helps your understanding.

System.out is a normal static attribute, it was set by the JVM through the initializeSystemClass() method during JVM initialization. You can even change it (although it's not recommended) by simply calling System.setOut(printOutStream); , where printOutStream is the stream you want to use as standard output.

Here's a nice article detailing how does System.out.println() work.

Out in System.out.pritln is a static field (object) of PrintWriter in System class and println is a method of PrintWriter.

Reference : System : http://docs.oracle.com/javase/6/docs/api/java/lang/System.html PrintWriter : http://docs.oracle.com/javase/6/docs/api/java/io/PrintWriter.html

There is no need to go for net and documentation even. We can simply say javap java.lang.System this gives you list of all static fields, method prototypes that belong to System class. We can get details of any java class using javap, provided you know its package and classname out is public static object of PrintStream defined in System class.

When System class get initialized, it calls its initializeSystemClass() method, here is the code:

FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));

In this code setOut0() is a native function implemented in System.c:

JNIEXPORT void JNICALL
Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream)
{
    jfieldID fid =
        (*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;");
    if (fid == 0)
        return;
    (*env)->SetStaticObjectField(env,cla,fid,stream);
}

This is a standard JNI code that sets System.out to the argument passed to it, this method calls the native method setOut0() which sets the out variable to the appropriate value.

System.out is final, it means it cannot be set to something else in initializeSystemClass() but using native code it is possible to modify a final variable.

System.out is provided by the JVM. By the time your main method is called, System.out is open and ready for use.

See http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#out

In the Oracle Java runtime libraries, it is instantiated natively using the registerNatives() native method which is called (via a static initializer) on loading the System class. This is however an implementation detail.

You can also set System.out directly using System.setOut() .

System.out.println();

here println is an object of printstream class.We can't directly create object for printstream class. Out is an object of system class. out is called field in system class. while calling system.out it indirectly creates object for printstream class. hence we can call println() method using System.out.prontln().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM