System.out.println() in Java

What Is System Out Println in Java?

To define System.out.println() in Java, it is a simple statement that prints any argument you pass and adds a new line after it. 

  • println() is responsible for printing the argument and printing a new line
  • System.out refers to the standard output stream

(System.out.println is a Java statement that prints the argument passed, into the System.out which is generally stdout.

  1. System is a Class
  2. out is a Variable
  3. println() is a method

System is a class in the java.lang package . The out is a static member of the System class, and is an instance of java.io.PrintStream . The 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.

the System class belongs to java.lang package)

Structure of System Out Println in Java:

The statement System.out.println contains three major parts: System, out, and println. 

  • System refers to a final class that we can find in the java.lang.package and this System class has a public and static member field called PrintStream. 
  • All instances of the class PrintStream have a public method called println()
  • out is an instance of the PrintStream class 

The structure of the statement System.out.println(), being in line with the standards, reflects all these relationships.

Syntax:

System.out.println(parameter)

Parameters: 

The parameter might be anything that the user wishes to print on the output screen.

System.in:

This is the standard input stream that is used to read characters from the keyboard or any other standard input device.

System.err:

This is the standard error stream that is used to output all the error data that a program might throw, on a computer screen or any standard output device.

Overloads of println() method:

Method Overloading in Java allows different methods to have the same name, but different signatures or parameters where each signature can differ by the number of input parameters or type of input parameters or both. From the use of println() we observed that it is a single method of PrintStream class that allows the users to print various types of elements by accepting different type and number of parameters.

Performance Analysis of System.out.println():

println() is a method that helps display output on a console. This might be dependent on various factors that drives the performance of this method. The message passed using println() is passed to the server’s console where kernel time (Kernel time is the execution time of a process spent in the kernel. e.g. for networking, disk I/O or other kernel tasks.) is required to execute the task. Kernel time refers to the CPU time. Since println() is a synchronized method, so when multiple threads are passed could lead to the low-performance issue. System.out.println() is a slow operation as it incurs heavy overhead on the machine compared to most IO operations.

There is an alternative way of performing output operations by invoking PrintWriter or the BufferedWriter class.

Difference Between print() and println(): System.out.print() vs. System.out.println():

While both the methods print the arguments passed to them and are available under System.out, there’s a difference between the two:

Reference:

https://www.interviewkickstart.com/learn/system-out-println-in-java#:~:text=Register%20for%20Webinar-,What%20Is%20System%20Out%20Println%20in%20Java%3F,and%20printing%20a%20new%20line

https://www.geeksforgeeks.org/system-out-println-in-java/

https://www.javatpoint.com/system-out-println-in-java

https://serverfault.com/questions/44456/what-is-kernel-times#:~:text=Kernel%20time%20is%20the%20execution,O%20or%20other%20kernel%20tasks.&text=Save%20this%20answer.,-Show%20activity%20on

https://beginnersbook.com/2021/08/system-out-println-in-java-explained-with-examples/

https://linuxhint.com/print-output-java/

Leave a comment

Design a site like this with WordPress.com
Get started