close
The Wayback Machine - https://web.archive.org/web/20240529030045/https://www.geeksforgeeks.org/system-out-println-in-java/
Open In App

System.out.println in Java

Last Updated : 12 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

System and PrintStream package

Syntax:

System.out.println(parameter)

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

Example of Java System.out.println()

Example 1:

Below is the implementation of System.out.println :

Java




// Java code to illustrate
// System.out.println();
import java.io.*;
  
// Driver Class
class GFG {
      // main function
    public static void main(String[] args)
    {
        System.out.println("Welcome");
        System.out.println("To");
        System.out.println("GeeksforGeeks");
    }
}


Output

Welcome
To
GeeksforGeeks

Example 2:

Below is the implementation of System.out.println :

Java




// Java code to illustrate
// System.out.println();
import java.io.*;
  
// Driver Class
class GFG {
      // main function
    public static void main(String[] args)
    {
        // Declaring variable
        int num1 = 10, num2 = 20, sum;
  
        // Printing the variables
        System.out.print("The addition of ");
        System.out.print(
            num1 + " and " + num2 + " is: ");
  
        // Printing the result after operation
        System.out.println(num1 + num2);
    }
}


Output

The addition of 10 and 20 is: 30

Just like System.out, Java provides us with two other standard or default input-output streams:

  1. System.in: This is the standard input stream that is used to read characters from the keyboard or any other standard input device. Example:
    InputStreamReader inp = new InputStreamReader(System.in);
  2. 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.
    Example:
    System.err.print("Error");

Overloads of println() method

As we know, 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.

For example:

System.out.println(), 
System.out.println(int),
System.out.println(double),
System.out.println(string),
System.out.println(character),
etc.

PrintStream has around 10 different overloads of println() method that are invoked based on the type of parameters passed by the user.

Example:

Java




// Java code to illustrate method
// overloading in println()
import java.io.*;
  
// Driver Class
class PrintLN {
      // main function
    public static void main(String[] args)
    {
        // Declaring different datatypes
        int num = 10;
        char ch = 'G';
        String str = "GeeksforGeeks";
        double d = 10.2;
        float f = 13.5f;
        boolean bool = true;
  
        // Various overloads of println() method
        System.out.println();
        System.out.println(num);
        System.out.println(ch);
        System.out.println(str);
        System.out.println(d);
        System.out.println(f);
        System.out.println(bool);
        System.out.println("Hello");
    }
}


Output

10
G
GeeksforGeeks
10.2
13.5
true
Hello

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:

  1. Difference between print() and println()
  2. Input-Output in Java
  3. PrintStream println() method in Java with Examples
  4. Redirecting System.out.println() output to a file in Java


Previous Article
Next Article

Similar Reads

Difference Between System.out.println() and System.err.println() in Java
System.out is a PrintStream to which we can write characters. It outputs the data we write to it on the Command Line Interface console/terminal. It is mostly used for console applications/programs to display the result to the user. It can be also useful in debugging small programs. Syntax: System.out.println("Your Text which you want to display");
2 min read
Difference Between System.out.print() and System.out.println() Function in Java
In Java, we have the following functions to print anything in the console. System.out.print() andSystem.out.println() But there is a slight difference between both of them, i.e. System.out.println() prints the content and switch to the next line after execution of the statement whereas System.out.print() only prints the content without switching to
2 min read
Redirecting System.out.println() Output to a File in Java
System.out.println() is used mostly to print messages to the console. However very few of us are actually aware of its working mechanism. We can use System.out.println() to print messages to other sources too, not just restricting it to the console. However, before doing so, we must reassign the standard output by using the following method of Syst
2 min read
Difference between print() and println() in Java
print(): print() method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of String. 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. Various print() methods: void print(boolean b) - P
3 min read
PrintWriter println(String) method in Java with Examples
The println(String) method of PrintWriter Class in Java is used to print the specified String on the stream and then break the line. This String is taken as a parameter. Syntax: public void println(String string) Parameters: This method accepts a mandatory parameter string which is the String to be printed in the Stream. Return Value: This method d
2 min read
PrintWriter println(Object) method in Java with Examples
The println(Object) method of PrintWriter Class in Java is used to print the specified Object on the stream and then break the line. This Object is taken as a parameter. Syntax: public void println(Object object) Parameters: This method accepts a mandatory parameter object which is the Object to be printed in the Stream. Return Value: This method d
2 min read
PrintWriter println() method in Java with Examples
The println() method of PrintWriter Class in Java is used to break the line in the stream. This method do not accepts any parameter or return any value. Syntax: public void println() Parameters: This method do not accepts any parameter. Return : This method do not returns any value. Below methods illustrates the working of println() method: Program
2 min read
PrintWriter println(char[]) method in Java with Examples
The println(char[]) method of PrintWriter Class in Java is used to print the specified character array on the stream and then break the line. This character array is taken as a parameter. Syntax: public void println(char[] charArray) Parameters: This method accepts a mandatory parameter charArray which is the character array to be printed in the St
2 min read
PrintStream println(float) method in Java with Examples
The println(float) method of PrintStream Class in Java is used to print the specified float value on the stream and then break the line. This float value is taken as a parameter. Syntax: public void println(float floatValue) Parameters: This method accepts a mandatory parameter floatValue which is the float value to be written on the stream. Return
2 min read
PrintStream println(long) method in Java with Examples
The println(long) method of PrintStream Class in Java is used to print the specified long value on the stream and then break the line. This long value is taken as a parameter. Syntax: public void println(long longValue) Parameters: This method accepts a mandatory parameter longValue which is the long value to be written on the stream. Return Value:
2 min read
Practice Tags :