What's the meaning of System.out.println in Java?

Javasystem.out

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

> System.out.println()

High level Understanding

For understanding this we need to recall few basics of java:

  • dot (.) operator in java: In java . (dot operator) is used only to call methods or variables. So we can say out is either method or variable.
  • Methods in java : we know methods always have parenthesis ‘( )’ after method name, So out cannot be a method in java. So out its a variable and println() is a method.
  • Class name in java: Class name should start with Capital letter ideally in java, So System is a class.

Now with basic knowledge of java we know :

  • System is a Class
  • out is a Variable
  • println() is a method

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

Solution 4 - Java

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.

Solution 5 - Java

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");

Solution 6 - Java

System.out.println("Hello World");
  1. 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.

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

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

Solution 7 - Java

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

Solution 8 - Java

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.

Solution 9 - Java

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 instantiated
  • out 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!

Solution 10 - Java

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();
}

Solution 11 - Java

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

Solution 12 - Java

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:

  1. print()
  2. println()
  3. 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.

Solution 13 - Java

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.

Solution 14 - Java

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.

Solution 15 - Java

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.

Solution 16 - Java

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);

Solution 17 - Java

System is the java class.

out is the instance and also static member of PrintStream.

println is the method of PrintStream.

Solution 18 - Java

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 http://www.amazon.com/Programming-Java¿-Virtual-Machine-Joshua/dp/0201309726/ref=sr_1_1?ie=UTF8&qid=1462724685&sr=8-1&keywords=Programming+java+virtual">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).

Solution 19 - Java

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 : http://tutorialscrux.com/java/systemoutprintln/system-out-println-java-explained/">How System.out.println() Works In Java

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprosseekView Question on Stackoverflow
Solution 1 - JavakennytmView Answer on Stackoverflow
Solution 2 - JavacHaoView Answer on Stackoverflow
Solution 3 - JavaVeKeView Answer on Stackoverflow
Solution 4 - JavaYoKView Answer on Stackoverflow
Solution 5 - JavaOmprakash.KView Answer on Stackoverflow
Solution 6 - JavaIsabella EngineerView Answer on Stackoverflow
Solution 7 - Javabeing_jView Answer on Stackoverflow
Solution 8 - JavaDushyantView Answer on Stackoverflow
Solution 9 - JavaShyamView Answer on Stackoverflow
Solution 10 - Javauser1527047View Answer on Stackoverflow
Solution 11 - JavapavanView Answer on Stackoverflow
Solution 12 - JavaBlasankaView Answer on Stackoverflow
Solution 13 - Javamohd zeeshanView Answer on Stackoverflow
Solution 14 - Javakuldeep rawatView Answer on Stackoverflow
Solution 15 - Javauser3819423View Answer on Stackoverflow
Solution 16 - JavaNagaraja G DevadigaView Answer on Stackoverflow
Solution 17 - JavaRavi RupaparaView Answer on Stackoverflow
Solution 18 - JavaprosseekView Answer on Stackoverflow
Solution 19 - JavaMantuView Answer on Stackoverflow