System.out.println(); Implementation - java

Can anyone here tell me how System.out.println() is implemented? I have tried many sites but didn't get any satisfactory answer.

In System.out.println,
System is a final class.
out is object of PrintStream class inside system.
println() is a method of PrintStream
So to print something we need to call a println which is inside PrintStream class whose object is inside System class.Hence
System.out.println();
For more information refer following
link

Related

Why doesn't System.out return a null object? [duplicate]

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

What is the type of System.out in Java?

I am just a newbie in Java. I was wondering the way System.out.println() is used. Out is a static field inside System class. The type of out is PrintStream. But when I saw the constructor of PrintStream class, it takes a parameter of type OutputStream and as far as I know we cannot create the object of an abstract class. In that case we must pass some subclass's object to the constructor of PrintStream. What is that class? Same is the System.in. It is also InputStream's reference but what is the type of object it points to as the InputStream is abstract?
PrintStream wraps BufferedOutputStream, which wraps FileOutputStream, which is writing into the console, which has its own FileDescriptor.
A simple way to view the structure of a class is to examine it in a debugger.
As you can see #Andremonify's description is basically what you have.
FileDescriptor
0 is System.in
1 is System.out
2 is System.err
3+ is used for other files
Yes out is of PrintStream type. And constructor of PrintStream takes OutputStream type. OutputStream is abstract class. But any superclass refrence can refer subclass object without casting, so PrintStream's constructor has OutputStream refrence, but this refrence must be referring one of OutputStream's subclass like FileOutputStream
There are a couple more things to say about the implementation of System.out.
The actual implementation class of System.out is not specified. The javadocs don't say what it is. We observe (in various way) that Oracle Java and OpenJDK Java implement the "stack" in a particular way (see other answers), but this could change in the future.
The System::setOut(PrintStream) method can be used to modify what System.out is bound to. If that happens, any assumptions about implementation classes may be incorrect.
It turns out that you can do this:
System.setOut(null);
so System.out could be null. However, with current implementations, System.out won't be null unless you set it to null.
All that is actually guaranteed by the specifications is that the value of System.out will have a type that is assignment compatible with PrintStream.

In Java, how does System.out refer to PrintStream class?

I'm a beginner learning Java with some knowledge of C++, and the System.out.println(); is confusing me right now. So System is the class, out is a variable that can call a method?? According to: http://journals.ecs.soton.ac.uk/java/tutorial/getStarted/application/objects.html
out is a class variable, and a variable is a storage location in the computer memory that has a type name and content. It's not an object like string that can use methods like .getLength(). The way the website explains it is that out refers to an instance of PrintStream class, but how?
It's not an object
This is where your reasoning is going wrong. System.out is (a reference to) an object.
The type of the reference is PrintStream, as documented in the Javadoc. This means that you can call PrintStream's methods on System.out, e.g.:
System.out.println();
out doesn't call a method : out is a variable holding an object (an instance of PrintStream) on which you can call a method.
For example :
System.out.println("hey!");
You could also do
void print(PrintStream ps, Object o) {
ps.println(o);
}
...
print(System.out, "hey!");
More strictly, it's a public static field that is a reference to an object of type PrintStream, so yes, you can call methods on it.
Java references are roughly analogous to C pointers (at least in the way they are used, obviously there are significant differences).
Out is a public static field of the class named System.
Because it's public, you can call methods on it.
Its type is PrintStream.
The best way to learn is to read the documentation:
http://docs.oracle.com/javase/6/docs/api/java/lang/System.html
Notice the part at the top where it describes in, out, and err.
System.out is a particular instance of PrintStream, whose output is linked to the equivalent of the C++ stdout
System is a final class which has a final variable out holding a object of PrintStream class on which we can call println() method.
Actually the out parameter in System.out.println(String args[]) is a static field in System class. Whenever a field is declared it should have certain datatype. In this class the out field is defined as static PrintStream out;.
It means the datatype of out is PrintStream class.In this way System.out will actually represents a object of PrintStream class. With this object we are calling println() method of PrintStream class.
Think of System class roughly like this :
package java.lang;
public final class System {
public final static PrintStream out;
}
here out is a static final variable of "type" Printstream.
Since it is a static variable, we can call it by "ClassName.variableName" without creating any object of System class, so we do System.out.
Now, out is a reference variable of "PrintStream" class. Till now, only this reference variable is created and it is not referring to any "object" of Printstream class.
But System class creates object of PrintStream when it is loaded in memory.
For this, see methods initializeSystemClass() and setOut0() in below link which is complete source code of System class. (Dont be overwhelmed by this enormous code, just be assured that there is "new PrintStream()" called inside System (here at line 1095)).
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/System.java
so, when we have an object of PrintStream class, our out reference variable can easily call a method on it, right? This is how we call System.out.println()

Why cant I create the object of Console class?

I am trying to create an object of Console class, but could not succeed. I am getting this error "The constructor Console() is not visible". I also could not find any constructor in the document. Can anybody explain this?
Jon Skeet's answer is correct: you should use System.console() to get hold of the one instance of Console.
The reason for this is that Console is an implementation of the Singleton design pattern, which is used when the author of an API wants to ensure that one and only one instance of a class is ever created.
You're not meant to create instances directly. Instead, use the System.console() method:
Console console = System.console();
You cannot create a Console object, the constructor is private.
But you can obtain a Console object in this way:
Console console = System.console();
Look at the javadoc for more info.
You dont need to create the object of the Console class. It is not required. Please read below from documentation :
If this virtual machine has a console then it is represented by a
unique instance of this class which can be obtained by invoking the
System.console() method. If no console device is available then an
invocation of that method will return null.
So, you will get the console by System.console() if any console device is available with Virtual Machine.
A Console can't be instantiated explicitely while it's constructor is private. You can access the console associated with the JVM the following way:
Console console = System.console();
See the documentation here.

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

Categories