What is System, out, println in System.out.println() in Java [duplicate]
Asked Answered
O

3

39

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

I was looking for the answer of what System, out and println are in System.out.println() in the Java. I searched and found a different answer like these:

  • System is a built-in class present in java.lang package. This class has a final modifier, which means that, it cannot be inherited by other classes. It contains pre-defined methods and fields, which provides facilities like standard input, output, etc.

  • out is a static final field (ie, variable)in System class which is of the type PrintStream (a built-in class, contains methods to print the different data values). static fields and methods must be accessed by using the class name, so ( System.out ).

  • out here denotes the reference variable of the type PrintStream class.

  • println() is a public method in PrintStream class to print the data values. Hence to access a method in PrintStream class, we use out.println() (as non static methods and fields can only be accessed by using the refrence varialble)

In another page i find another contrasting definition as

System.out.print is a standard output function used in java. where System specifies the package name, out specifies the class name and print is a function in that class.

I am confused by these. Could anybody please exactly tell me what they are?

Oligochaete answered 17/8, 2012 at 8:15 Comment(8)
See https://mcmap.net/q/301400/-what-39-s-the-meaning-of-system-out-println-in-java/396730 and https://mcmap.net/q/408958/-just-wondering-system-out-println/396730Heptagonal
I think the first explanation is somewhat clearer, and you can always browse the source to see for yourself, e.g. public final static PrintStream out = nullPrintStream();Prague
The second explanation is just plain wrong.Heptagonal
@Prague The second explanation is wrong, since System is a class, not a package and out is a field, not a class.Geesey
I agree with you both, marking to close as dupe of that first question.Transmittal
yep , the second one really sucksPrague
System.getClass() should tell the truth, shouldn't it ?Camber
@sylvainulg: that won't work.Padraig
A
8

The first answer you posted (System is a built-in class...) is pretty spot on.

You can add that the System class contains large portions which are native and that is set up by the JVM during startup, like connecting the System.out printstream to the native output stream associated with the "standard out" (console).

Amphibian answered 17/8, 2012 at 8:22 Comment(0)
R
115

System is a final class from the java.lang package.

out is a class variable of type PrintStream declared in the System class.

println is a method of the PrintStream class.

Root answered 17/8, 2012 at 8:23 Comment(0)
O
11

Whenever you're confused, I would suggest consulting the Javadoc as the first place for your clarification.

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. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user.
For simple stand-alone Java applications, a typical way to write a line of output data is:

     System.out.println(data)
Optime answered 17/8, 2012 at 8:22 Comment(3)
Consulting the Javadoc is fine when you understand what they say. But if you do not? For instance a newbie might not understand the concepts of standard output, and error output streams and that a stream is already open and ready to accept output data. Of course the newbie might google for this concepts, but that often leads to new concepts he does not understand ad infinitum. I am not telling there is anything wrong with Javadocs. I'm just pointing out the difficulties a newbie might have. That is why he often turns to SO to get explanation in a language that he can undersand.Aberration
@BobUeland: When you don't understand something, IMO, the best possible way is to look at the documentation of it, rather than looking at people's interpretation of the same thing. When people interpret official documents in their own words, that at times might become a source of confusion in itself. I mean there're a lot of ways to explain the same thing in your own words & if there're multitude of answers, chances of that being confusing in itself is pretty high.Optime
I agree with you, but newbies who do not understand docs want/need something like this: "Look at the System.out.println(). The first one begins with capital letter so its a class. The third one has () so it's a method. The second one begins with lowercase letter and has no (), so it is a field. You want to print something but must find the guy who can do the job for you. You do not know that guy, byt you know the System, who has a field called out, that points to the guy. So you know that System.out poits to the guy, and you know that this guy has the println() method.Aberration
A
8

The first answer you posted (System is a built-in class...) is pretty spot on.

You can add that the System class contains large portions which are native and that is set up by the JVM during startup, like connecting the System.out printstream to the native output stream associated with the "standard out" (console).

Amphibian answered 17/8, 2012 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.