Java Output -

Java Output

In Java, you can use the println() and print() methods to print output.

Java println()

The println() method always prints output on a new line. You can use it as many times as you need.

Java println() Example

public class Test {
  // main method.
  public static void main(String[] args) {
    System.out.println("Hello learners");
    System.out.println("Here you will learn programming tutorials");
    System.out.println("Also programming related blogs.");
  }
}

Output:

javac Test.java
java Test
Hello learners
Here you will learn programming tutorials
Also programming related blogs.

In the example, you can see that each println() prints on a new line.

Java print()

The print() method also prints output, but it does not start a new line. It keeps printing on the same line.

Java print() Example

public class Test {
  // main method.
  public static void main(String[] args) {
    System.out.print("Hello learners ");
    System.out.print("Here you will learn programming tutorials ");
    System.out.print("Also programming related blogs.");
  }
}

Output:

javac Test.java
java Test
Hello learners Here you will learn programming tutorials Also programming related blogs.

In this example, print() does not start a new line after each output.

New Line with print()

However, you can use \n with print() to print on a new line.

public class Test {
  // main method.
  public static void main(String[] args) {
    System.out.print("Hello learners \n");
    System.out.print("Here you will learn programming tutorials \n");
    System.out.print("Also programming related blogs.");
  }
}

Print Numbers

You can also print numbers or expressions using both println() and print().

public class Test {
  public static void main(String[] args) {
    System.out.println(420);
    System.out.println(5 * 4);
  }
}

Output:

javac Test.java
java Test
420
20

I hope you now understand how to print output in Java.

Leave a Comment