Is this static println
function in out
class from System
namespace?
namespace System { class out { static println ... }
How can I interpret this name? And where in JRE this function is defined? In java.lang.System
/java.lang.Object
?
Is this static println
function in out
class from System
namespace?
namespace System { class out { static println ... }
How can I interpret this name? And where in JRE this function is defined? In java.lang.System
/java.lang.Object
?
It is quite simple to understand the question, but to answer it we need to dig deeper in to Java native code.
System
is static class and cannot be instantiatedout
is a reference variable defined inSystem
println()
is the method used to print on standard output.A brief and nice explanation is always welcome on this as we can learn much from this single line of statement itself!
System
is a class.The out variable (referred to above as
System.out
) is a class variable of the System class.The
PrintStream
class is in a package of classes that are used to provide stream I/O capability for Java.The
out
variable in theSystem
class refers to (points to) an instance of thePrintStream
class (aPrintStream
object) which is automatically instantiated when theSystem
class is loaded into the application. ThePrintStream
class has an instance method namedprintln()
which causes its argument to be displayed on the standard output device.High level Understanding
For understanding this we need to recall few basics of java:
Now with basic knowledge of java we know :
Lets get more in details:
out variable: static or instance?
called using class name, so we know its static variable of System class.
but its calling a method println() method so ‘out’ is an object of the reference type PrintStream.
the System class belongs to java.lang package
the Prinstream class belongs to java.io package
println
andprint
are the two overloaded methods which belong to thePrintStream
class.To access them we need an instance of this class.
A static property called
out
of typePrintStream
is created on theSystem
class.Hence to access the above methods we use the following statements:
System.out.println("...") in Java code is translated into JVM. Looking into the JVM gave me better understanding what is going on behind the hood.
From the book Programming form the Java Virtual Machine. This code is copied from https://github.com/ymasory/programming-for-the-jvm/blob/master/examples/HelloWorld.j.
This is the JVM source code.
As "The JVM doesn't permit byte-level access to memory" the
out
object in type Ljava/io/PrintSteram
; is stored in a stack withgetstatic
JVM command. Then the argument is pushed on the stack before called a methodprintln
of thejava/io/PrintStream
class from an instance namedout
. The method's parameter is (Ljava/lang/String
;) and output type is void (V).System
is a class injava.lang package
. Andout
is aPrintStream
object. Nice explanation @ http://lazy-geeks.blogspot.in/2015/01/what-is-systemoutprintln.html