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 Sequence | Description | Code |
|---|---|---|
| \n | New line | System.out.println("Hello\nWorld"); |
| \t | Tab - prints four spaces | System.out.println("Name:\tJohn"); |
| \b | Backspace | System.out.println("Hello\bWorld"); |
| \f | Form feed | System.out.println("Hello\fWorld"); |
| \r | Carriage return | System.out.println("Hello\rWorld"); |
| \ | It adds backslash | System.out.println("This is a backslash: \\"); |
| ' | Single quote | System.out.println("This is a single quote: \'"); |
| " | Double quote | System.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