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

19

63

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?

Aloud answered 4/8, 2010 at 14:48 Comment(2)
JDK is open source. Pick an IDE, include the JDK source and Ctrl+Click your way through the System.out :)Liquidation
@prosseek, i will suggest you to use netbeans, and just type System.out.println(); on it and place mouse cursor over System and press control key and mouse left click too, you will get the definition of System class and then everything will be cleared to you... :)Shed
F
88

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

Fanchette answered 4/8, 2010 at 14:50 Comment(5)
+1 for referencing Java naming convention as questioner is coming from other language(s).Inexpensive
Also note that System.java is slightly misleading as in,out and err are set by "magic".Feudalize
@Dimitris: Hehe because that's the first hit from Google. Updated to SE 6.Fanchette
What is meant by "not as in .NET"?Ouidaouija
@Fabian: The .net BCL has a 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
B
48

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.

Bealle answered 4/8, 2010 at 14:52 Comment(1)
great explanationNormalcy
T
28

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

Tarim answered 27/11, 2014 at 10:40 Comment(1)
Best answer. ThanksRema
F
27

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.

Felizio answered 4/8, 2010 at 15:10 Comment(0)
S
7

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");
Syncytium answered 21/12, 2011 at 7:6 Comment(0)
B
4
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()

Bimolecular answered 7/7, 2013 at 23:15 Comment(0)
P
2

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

Prehuman answered 26/4, 2012 at 11:30 Comment(0)
C
2

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.

Chandlery answered 4/9, 2014 at 13:12 Comment(0)
W
1

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!

Will answered 18/8, 2012 at 8:56 Comment(0)
P
1

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();
}
Pelfrey answered 17/7, 2014 at 9:22 Comment(0)
C
1

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

Comedo answered 1/1, 2015 at 16:27 Comment(0)
I
1
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.

Imminent answered 20/7, 2017 at 14:56 Comment(0)
C
0

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.

Coworker answered 24/8, 2012 at 2:17 Comment(1)
I think you mean overridden, not overloaded.Pursuer
S
0
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.

Scatterbrain answered 29/3, 2014 at 16:28 Comment(0)
P
0

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.

Perisarc answered 9/7, 2014 at 7:54 Comment(0)
C
0

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);
Cm answered 10/12, 2014 at 6:47 Comment(0)
K
0

System is the java class.

out is the instance and also static member of PrintStream.

println is the method of PrintStream.

Killy answered 21/9, 2015 at 13:13 Comment(0)
A
0

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

Aloud answered 8/5, 2016 at 16:29 Comment(0)
G
-1

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

Ginsberg answered 17/9, 2014 at 9:16 Comment(1)
Hi welcome to stack overflow! This question already has a broadly accepted answer. While it is okay to post answers on old questions, make sure it has a high relevance and that it contributes something new. Also, please keep in mind that posting links to external websites is discouraged because the link can get outdated. Happy coding!Anorthite

© 2022 - 2024 — McMap. All rights reserved.