Printing Output

System.out.println() / System.out.print() statements are used to print on screen. Everything we want to write on screen is enclosed in double quotation marks.

We can add aa many print statements as we want. The difference between System.out.println() and System.out.print() is that the println statement print your text and the next statement will be printed on new line while in case of print statement, the next statement is printed on the same line.

Adding Line Breaks

"\n" is used to print new line.

Escape Sequences

Escape SequenceDescriptionCode
\nNew lineSystem.out.println("Hello\nWorld");
\tTab - prints four spacesSystem.out.println("Name:\tJohn");
\bBackspaceSystem.out.println("Hello\bWorld");
\fForm feedSystem.out.println("Hello\fWorld");
\rCarriage returnSystem.out.println("Hello\rWorld");
\It adds backslashSystem.out.println("This is a backslash: \\");
'Single quoteSystem.out.println("This is a single quote: \'");
"Double quoteSystem.out.println("She said, \"Hello!\"");

Printing Numbers

If we want to print numbers on screen, we do not enclose them in double quotation marks.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

Which of the following is a best practice in programming?

10 XP~2 min
Exercise 2 of 2Easy

Code that is easy to read and understand is called ___ code.

10 XP~2 min