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
?
No. Actually out
is a static member in the System
class (not as in .NET), being an instance of PrintStream
. And println
is a normal (overloaded) method of the PrintStream
class.
See http://download.oracle.com/javase/6/docs/api/java/lang/System.html#out.
Actually, if out
/err
/in
were classes, they would be named with capital character (Out
/Err
/In
) due to the naming convention (ignoring grammar).
System
namespace. If we ignore naming conventions for a second, System.out
may well be a class named out
in the System
namespace, as the OP was first thinking. That's not how it is in Java. –
Bealle System
is a class, that has a public static field out
. So it's more like
class System
{
public static PrintStream out;
}
class PrintStream
{
public void println ...
}
This is a slight oversimplification, as the PrintStream
class is actually in the java.io
package, but it's good enough to show the relationship of stuff.
System.out.println()
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
class System {
public static final PrintStream out;
//...
}
the Prinstream class belongs to java.io package
class PrintStream{
public void println();
//...
}
Explained answer on my youtube what is System.out.println
Check following link:
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/System.html
You will clearly see that:
System
is a class in the java.lang
package.
out
is a static member of the System
class, and is an instance of java.io.PrintStream
.
println
is a method of java.io.PrintStream
. This method is overloaded to print message to output destination, which is typically a console or file.
println
and print
are the two overloaded methods which belong to the PrintStream
class.
To access them we need an instance of this class.
A static property called out
of type PrintStream
is created on the System
class.
Hence to access the above methods we use the following statements:
System.out.println("foo");
System.out.print("foo");
System.out.println("Hello World");
System
: It is the name of standard class that contains objects
that encapsulates the standard I/O devices of your system.It is contained in the package java.lang
. Since java.lang
package is imported in every java program by default,therefore java.lang
package is the only package in Java API which does not require an import declaration.
out
:The object out represents output stream(i.e Command
window)and is the static data member of the class
System
.So note here System.out
(System
-Class & out
- static object i.e why its simply referred to by classname and we need not create any object).
println
:The println()
is method of out
object that
takes the text string as an argument and displays it to the standard
output i.e on monitor screen.Note
System
-Class
out
-static Object
println()
-method
Remember a function (in java function is called method) always has the format function()
• System
is a class in java.lang
package
• out
is a static
object of PrintStream
class in java.io
package
• println()
is a method in the PrintStream
class
System
is a class of java.lang
package, out
is an object of PrintStream
class and also static
data member of System
class, print()
and println()
is an instance method of PrintStream
class.
it is provide soft output on console.
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 in System
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!
Because out is being called with the System
class name itself, not an instance of a class (an object), So out
must be a static variable belonging to the class System
. out
must be instance of a class, because it is invoking the method println()
.
// the System class belongs to java.lang package
class System {
public static final PrintStream out;
}
class PrintStream {
public void println();
}
System
is a class in java.lang package
. And out
is a PrintStream
object. Nice explanation @ http://lazy-geeks.blogspot.in/2015/01/what-is-systemoutprintln.html
System.out.println();
System
is the class
out
is a variable in the System
class and it is a static
and variable type is PrintStream
.
Here is the out
variable in System
class:
public final static PrintStream out = null;
You can see implementation of System
here.
println()
is a overloaded method in PrintStream
class.
PrintStream
includes three overloaded printing methods, those are:
print()
println()
printf()
You can see implementation of PrintStream
here.
You cannot instantiate System
class and it is child class of Object
and the Object
is the father(superclass) of every classes including classes that you defined.
Here is what the oracle docs says:
public final class System extends Object
The
System
class contains several useful class fields and methods. It cannot be instantiated.Among the facilities provided by the
System
class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.Since: JDK1.0
If you donot know what is meant by instantiate, read this questioh. It is C# question but the concept is same.
Also, What is the difference between an Instance and an Object?
If you donot know what is meant by overload read this quesiotn.
System
is a class in java.lang
package.
out
is the static data member in System
class and reference variable of PrintStream
class.
Println()
is a normal (overloaded) method of PrintStream
class.
System.out.println
System
is a class in the java.lang
package.
out
is a static
data member of the System
class and references a variable of the PrintStream
class.
System
: is predefined class of java.lang
package.
out
: is a static
member of printStream
class and its connect with console.
Println
: is a method of printstream
class and its not a static
.
From the javadoc about System
, here's what the doc says:
public final class System
extends Object
The System class contains several useful class fields and methods. It cannot be instantiated.
Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
Since:
JDK1.0
Regarding System.out
public static final PrintStream out
The "standard" output stream class Prinstream belongs to java.io package. This stream is already open and ready to accept output data.
When the JVM is initialized, the method initializeSystemClass() is called that does exactly what it’s name says – it initializes the System class and sets the out variable. The initializeSystemClass() method actually calls another method to set the out variable – this method is called setOut().
Typically this stream corresponds to display output or another output destination specified by the host environment or user.
Regarding println();
class PrintStream{
public void println();
}
For simple stand-alone Java applications, a typical way to write a line of output data is:
System.out.println(data);
System is the java class.
out is the instance and also static member of PrintStream.
println is the method of PrintStream.
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.
.class public HelloWorld
.super java/lang/Object
.method public static main([Ljava/lang/String;)V
.limit stack 2
.limit locals 1
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "Hello, world"
invokevirtual java/io/PrintStream/println
(Ljava/lang/String;)V
return
.end method
.end class
As "The JVM doesn't permit byte-level access to memory" the out
object in type Ljava/io/PrintSteram
; is stored in a stack with getstatic
JVM command.
Then the argument is pushed on the stack before called a method println
of the java/io/PrintStream
class from an instance named out
. The method's parameter is (Ljava/lang/String
;) and output type is void (V).
System
- class which is final
in nature. public final class System{}
. Belongs to java.lang
package
out
- static
reference variable of type PrintStream
println()
- non static
method in PrintStream
class.
PrintStream
belongs to java.io
package.
To understand it better you can visit : How System.out.println() Works In Java
© 2022 - 2024 — McMap. All rights reserved.
System.out
:) – Liquidation