Difference Between print and println in Java



As we know in Java these both methods are primarily used to display text from code to console. Both these methods are of PrintStream class and are called on static member 'out' of 'System' class which is a final type class.

The following are the important differences between print() and println().

Sr. No. Key print() println()
1 Implementation print method is implemented as it prints the text on the console and the cursor remains at the end of the text at the console. On the other hand, println method is implemented as prints the text on the console and the cursor remains at the start of the next line at the console and the next printing takes place from next line.
2 Nature The prints method simply print text on the console and does not add any new line. While println adds new line after print text on console.
3 Arguments print method works only with input parameter passed otherwise in case no argument is passed it throws syntax exception. println method works both with and without parameter and do not throw any type of exception.

Example of print() vs println()

JavaTester.java

import java.io.*;
class JavaTester {
   public static void main(String[] args){
      System.out.print("Hello");
      System.out.print("World");
   }
}

Output

HelloWorld

Example

JavaTester.java

import java.io.*;
class JavaTester {
   public static void main(String[] args){
      System.out.println("Hello");
      System.out.println("World");
   }
}

Output

Hello
World
Updated on: 2019-09-18T14:23:00+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements