Difference between System.out.print() and System.out.println()

System.out.print()

This method prints the text on the console and the cursor remains at the end of the text at the console. The next printing takes place from just here. This method must take atleast one parameter else it will throw an error.

System.out.println()

This method prints the text on the console and the cursor remains at the start of the next line at the console. The next printing takes place from the next line. This method may or may not take any parameter.

Example:

Java




// Java code to illustrate difference
// between print() and println()
import java.io.*;
  
// Driver Class
class Demo_print {
      // main function
    public static void main(String[] args)
    {
        System.out.println("Using print()");
  
        // using print()
        // all are printed in the
        // same line
        System.out.print("GfG! ");
        System.out.print("GfG! ");
        System.out.print("GfG! ");
  
        System.out.println();
        System.out.println();
        System.out.println("Using println()");
  
        // using println()
        // all are printed in the
        // different line
        System.out.println("GfG! ");
        System.out.println("GfG! ");
        System.out.println("GfG! ");
    }
}


Output:

Using print()
GfG! GfG! GfG!

Using println()
GfG!
GfG!
GfG!

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 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. They are fast as compared to the println() of the PrintStream class.

Related Articles:



System.out.println in Java

Java System.out.println() is used to print an argument that is passed to it.

Parts of System.out.println()

The statement can be broken into 3 parts which can be understood separately:

  1. System: It is a final class defined in the java.lang package.
  2. out: This is an instance of PrintStream type, which is a public and static member field of the System class.
  3. println(): As all instances of the PrintStream class have a public method println(), we can invoke the same on out as well. This is an upgraded version of print(). It prints any argument passed to it and adds a new line to the output. We can assume that System.out represents the Standard Output Stream.

Syntax:

System.out.println(parameter)

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

Similar Reads

Example of Java System.out.println()

Example 1:...

Overloads of println() method

...

Difference between System.out.print() and System.out.println()

...