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
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.
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().
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.
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.