Unit 1: Output

Table of Contents

  1. Printing

Printing

You can use the System.out.println method to print something to the console.

// Prints Hello world! to the console
System.out.println("Hello world!");

Output:

Hello world!

The System.out.println method automatically adds a newline character (\n) to the end of whatever it prints. Basically that means that whatever you put in parentheses is printed on its own line.

On the other hand, if you want things to be printed right next to each other, use System.out.print.

System.out.println("This starts a new line");
System.out.print("This does");
System.out.print(" not");

Output:

This starts a new line
This does not

Another way to print things is using the System.out.printf method, which allows you to format output. We’ll cover this in more detail in Variables.

Note: Besides the newline character (\n), there are other special types of characters such as the tab character (\t) which inserts a tab.