Lesson 4 +10 XP

Java Output

Java Output

You print text to the screen with the System.out object. There are three main methods:

  • print() prints text without moving to a new line.
  • println() prints text and then moves to a new line.
  • printf() prints formatted text using placeholders.

println

System.out.println("Hello");
System.out.println("World");

Output:

Hello
World

print

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

Output (both stay on the same line):

Hello World

Printing numbers

You can print numbers directly, without quotes:

System.out.println(3);
System.out.println(358);
System.out.println(3 + 3);

printf with placeholders

int age = 25;
System.out.printf("I am %d years old", age);

Here %d is a placeholder for a number.

TL;DR

  • println() adds a new line after the text.
  • print() stays on the same line.
  • Numbers are printed without quotes.
  • printf() uses placeholders for formatted output.