out in System.out.println()
Asked Answered
P

8

21

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 ?

Prokopyevsk answered 26/2, 2012 at 16:34 Comment(2)
What do you mean by "System.out is returning null"?Fazio
I've created an analogical program like class A{ static B out}; class B{ } I tried to print System.out.println(A.out); which is returning null.Prokopyevsk
B
24

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.

Bander answered 26/2, 2012 at 16:58 Comment(0)
P
6

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.

Pula answered 26/2, 2012 at 16:40 Comment(1)
Actually out is set by the JVM through the initializeSystemClass() method.Bander
F
0

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

Fowkes answered 26/2, 2012 at 16:39 Comment(0)
G
0

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

Goldshlag answered 26/2, 2012 at 16:43 Comment(1)
Is this true? Because registerNatives() links native methods to JVM internal methods, I've never seen it used to instantiate variables.Bander
F
0

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

Fredericton answered 6/7, 2013 at 9:22 Comment(0)
W
0

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.

Worsted answered 10/1, 2015 at 4:19 Comment(0)
O
0

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.

Obmutescence answered 27/8, 2017 at 3:30 Comment(0)
I
-7

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

Introspect answered 26/2, 2012 at 16:42 Comment(6)
Lots of misstatements in this answer, almost too many to count. Ignore.Alten
Surendar, println() is not an object but a method of the PrintStream class.Eboni
You can directly create an object for the PrintStream class. For example, docs.oracle.com/javase/1.5.0/docs/api/java/io/… .Eboni
"out" is not specifically an object of System, but it is a field of System, as you say. Not "called", but it "is", as objects do not simply "have" objects in Java, it is more structured than that.Eboni
When calling System.out, the PrintStream object is not created indirectly; it actually existed when the JVM started up and its reference was assigned to System.out. (as mentioned in some other posts above)Eboni
Finally, you have spelled a few names incorrectly. In Java everything is case sensitive. You misspelled PrintStream, System, and println().Eboni

© 2022 - 2024 — McMap. All rights reserved.