Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 838
Ankush Dutt Avatar asked Nov 29 '18 10:11

Ankush Dutt


People also ask

What is the purpose of system out Println () method?

out. println(): This method prints the text on the console and the cursor remains at the start of the next line at the console.

What is the use of system out Println (); in Java programming give an example?

In java, we use System. out. println() statement to display a message, string or data on the screen. It displays the argument that we pass to it.

Why system out Println should not be used?

System. out. println is an IO-operation and therefor is time consuming. The Problem with using it in your code is, that your program will wait until the println has finished.


2 Answers

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.

like image 74
Ganesh chaitanya Avatar answered Sep 18 '22 14:09

Ganesh chaitanya


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.

like image 37
pri Avatar answered Sep 21 '22 14:09

pri