2. Generating Outputs in Java

Print statements

In Java, when we want to display output on the screen, we use the print() and println() methods. These methods are part of something called System.out, which simply means “send the output to the screen.”

  1. print() – This prints the text and keeps the cursor on the same line.


public class printStatements{
  public static void main(String[] args) {
    System.out.print("Hello ");
    System.out.print("World!");
  }
}
    
  • The words stay on the same line because print() does not move to the next line.


2. println() – This prints the text and moves to the next line.



public class printStatements{
  public static void main(String[] args) {
    System.out.println("Hello ");
    System.out.println("World!");
  }
}
    
  • Here, “World!” appears on a new line because println() moves the cursor to the next line after printing.

What is the difference between the print() and println() methods in Java?

Pages: 1 2 3