Open In App
Related Articles

Difference Between System.out.print() and System.out.println() Function in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In Java, we have the following functions to print anything in the console.

  • System.out.print() and
  • System.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 the next line after executing this statement.


The following examples will help you in understanding the difference between them more prominently.

Example 1:

Java

import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        System.out.println("Welcome to JAVA (1st line)"); 
        
          // this print statement will be printed in a new line.
        System.out.print("Welcome to GeeksforGeeks (2nd line) "); 
        
          // this print statement will be printed in the same line.
        System.out.print("Hello Geeks (2nd line)");
    }
}

                    

Output
Welcome to JAVA (1st line)
Welcome to GeeksforGeeks (2nd line) Hello Geeks (2nd line)

Example 2:

Java

import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        System.out.print("Welcome to JAVA (1st line) "); 
        
          // this print statement will be printed in the same line.
        System.out.println("Welcome to GeeksforGeeks (1st line)"); 
        
          // this print statement will be printed in a new line.
        System.out.print("Hello Geeks (2nd line)");
    }
}

                    

Output
Welcome to JAVA (1st line) Welcome to GeeksforGeeks (1st line)
Hello Geeks (2nd line)


Last Updated : 07 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads