System.out.println() in Java

System.out.println() is a statement in Java, it is used to print the argument passed to it. The println() method is a part of the java.io package (predefined classes and methods); it displays the result on the monitor. Usually, a method is invoked by an object name followed by a ‘.’ symbol and method name like “objectname.methodname()”. So here, in out.println(), out is the object name. But, we can create an object directly to PrintStream class like “PrintStream obj.print(“Hello”)”, so, Java provides an alternative way to create an object that is “System.out”. Consider the below image to clear a picture of objects and classes.

structure of System.out.println()

So, in the complete statement System is the class name, out is an instance of the System class and is of type PrintStream. The system.out.println() method always used with the public and final access specifiers.

When a member is called a PrintStream class object is created internally. So, we can call the print method as follows to print the argument:

System.out.print();

The above statement creates the PrintStream class object. by default, this object displays on the output devices like monitor.

Examples of System.out.println()

Let’s consider the following example:

class Demo

{

public static void main(String args[])

{

System.out.print("Hello!");

System.out.print("JavaSterling");

}

}

Output:

Hello! JavaSterling

Explanation:

In the above example, there are two print() methods, which are giving the output in one line because we have used two print methods. The first method is displaying the string “Hello” and holds the cursor on the same line. The second print() is displaying string “JavaSterling” at the same line. Let’s understand the print() and println() methods so that you can get why it is displaying both strings on the same line instead of a new line.

The print() and println() methods

The print() and println() both methods are used to display the output on the monitor. The print() method displays the results and holds the cursor on the same line but, the println() method moves the cursor to a new line. So, if we use the println() method, it will display the output on a new line.

Java println() method

As we have discussed the println() method is similar to the print() method. The only difference b/w both methods is that it moves the cursor to the next line after printing the result. It is used to display the result in two separate lines.

Consider below example:

class Demo
{
public static void main(String args[])
{
System.out.println("Hello!");
System.out.println("JavaSterling");
}
}

Output:

Hello!
JavaSterling

From the above example, the println() method is displaying the output in two separate lines.

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

System.out.print():

The system.out.print() method displays the output on the same line because of the print() method. So, the next print statement will print on the same line. This method must take at least one parameter, without parameter, it will throw an error message.

System.out.println():

The system.out.println() method displays the output on separated lines because of the println() method. So, the next print statement will print with a new line. It is not necessary to pass a parameter with it, so, This method may or may not take parameters.

Let’s consider the below example:

import java.io.*; 
  
class Test_print { 
    public static void main(String[] args) 
    { 
        System.out.println("Using print()"); 
        System.out.print("JavaSterling"); 
        System.out.print(" JavaSterling"); 
        System.out.print(" JavaSterling"); 
        System.out.println(); 
        System.out.println(); 
        System.out.println("Using println()");
        System.out.println(" JavaSterling "); 
        System.out.println(" JavaSterling "); 
        System.out.println(" JavaSterling "); 
    } 
} 

Output:

Using print()
JavaSterling JavaSterling JavaSterling
Using println()
JavaSterling
JavaSterling
JavaSterling