what is the purpose of using System.out.println() [closed]
Asked Answered
F

2

7

I was going through the internal implementation of System.out.println().Though I understood how this is working but couldn't figure out :

  1. Why they decided to use the System class in the first place.
  2. They could have directly used PrintStream class which is present in the io package.

  3. What is the significance of the syntax className.referenceVariable.Methodname, since we generally don't use this. Is there any specific reason for this.

Can anybody elaborate on these points or any related information would be great.

Fumigate answered 29/11, 2018 at 10:40 Comment(7)
The System.out stuff was first done before Java 1.0. In hindsight it was probably a mistake.Scream
If you declared a static variable of type Y in class X and wanted to call a method Z on the instance referenced by that variable, the syntax would be X.Y.Z(). There's nothing special about the className.referenceVariable.Methodname syntax.Pamilapammi
From what I understand, println doesn't / isn't part of the IO as it's there for output only. PrintStream requires IO access for whatever file is loaded in the stream to print. Waiting for a more detailed answer on this to learn more :)Grammatical
The availability of input, output and error streams is a property of the System in a similar way environment variables are.Glaudia
Why would all PrintStreams care about the system's standard IO streams? There are PrintStreams which operate on completely different data sinks (generic OutputStreams for example.)Canoness
@Pamilapammi Yes,I completely agree on this part but i wanted to know is there any specific reason when they could have directly used PrintStream from io package.I mean why to complicate things when they could have used some direct approach.Fumigate
What do you mean by "directly used PrintStream"? System.out and System.err are specialized PrintStreams. Don't get the abstraction here wrong: System.out is an instance of one special PrintStream, but PrintStream has no relationship to System.out or err or any other specific data sink.Canoness
F
1

According to my understanding, System class name was to indicate any interactions with System on which JVM is running. To perform Read or Write operations on the System Console, or reading environment variables in the System etc.

You can always use PrintStream directly as well there is no harm. But then you have to create a PrintStream object every time in your class and call methods inside use it. Instead its already created statically in System class for us to use it readily.

As for your 3rd query, Eran already answered it in the comments.

Furze answered 29/11, 2018 at 11:13 Comment(0)
A
1

The System class states that:

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 you were checking the source code of System class, you might've noticed that out object is final and is set to null. It's the setOut() method which assigns a value to the out variable (it's a native code).

I know, how can JVM set value to a final variable after it's been set to null, right? Being a JVM has its own advantages!

Before a value gets assigned to out object, a separate method called checkIO is also triggered, which checks for IO permission.

So System class was designed as a collection of standard input, output, and error streams. It also instructs JVM to initialise the objects, like the out object.

Regarding the syntax of System.out.println(), Eran has already explained it.

Antecedents answered 29/11, 2018 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.