Skip to lesson

Printing with println and print

Output is what your program shows on the screen. Java prints it with System.out.println() and System.out.print(), and the only difference is where the next output starts.

println and print

You want to print a canteen bill: a title, each item with its price, a gap, then the total. For that you must control where each line ends.

System.out.println() prints a value and then moves to the next line. System.out.print() prints a value and stays on the same line.

An empty System.out.println(); prints a blank line. Step through the bill below and watch the cursor, the place where the next character will appear: it stays put after print and jumps down after println.

Trace the code

Build a bill line by line

public class Bill {
public static void main(String[] args) {
System.out.println("Canteen bill");
System.out.print("Tea ");
System.out.println(20);
System.out.print("Samosa ");
System.out.println(30);
System.out.println();
System.out.print("Total: ");
System.out.println(20 + 30);
}
}

Variables

None yet.

Output

Canteen bill

Line 3println prints the title, then moves the cursor to the next line.

Step 1 of 8
print adds no space by itself. Put the space inside the text, as in "Tea ", or the words run together.

Escape sequences: new line and tab

You want names in one column and marks in another. The text may also need a ", which would normally end the String early.

An escape sequence is a backslash \ plus one character, and Java reads the pair as one special character.

Example 1 · A marks list with \t and \n

MarksList.javaJava
public class MarksList {    public static void main(String[] args) {        System.out.println("Name\tMarks");        System.out.println("Asha\t92");        System.out.println("Ravi\t85\n");        System.out.println("Teacher said, \"Well done\"");        System.out.println("Saved in C:\\java\\marks.txt");    }}
Output
Name	Marks
Asha	92
Ravi	85

Teacher said, "Well done"
Saved in C:\java\marks.txt

What this code does

  • \t is a tab: it jumps to the next column, like the Tab key, so the marks line up under Marks.
  • \n starts a new line. At the end of "Ravi\t85\n" it ends one more line, so a blank line follows.
  • \" prints a double quote and \\ prints one backslash, as in C:\java. The escaping backslash itself never shows.

Printing text and values together

A bill line such as Total: 175 mixes text and numbers. As in String basics (opens in a new tab), + joins them into one String for one println.

Java reads + left to right: once text comes first, every + after it joins instead of adding.

Example 2 · Text and values on one line

Report.javaJava
public class Report {    public static void main(String[] args) {        String name = "Priya";        int maths = 90;        int science = 85;        System.out.println(name + " scored " + maths + " in Maths");        System.out.println("Total: " + maths + science);        System.out.println("Total: " + (maths + science));    }}
Output
Priya scored 90 in Maths
Total: 9085
Total: 175

What this code does

  • name + " scored " + maths + " in Maths" joins the name, the text and the number into one line.
  • In "Total: " + maths + science the text comes first, so 90 and 85 are joined as text: 9085.
  • (maths + science) is in brackets, so Java adds 90 + 85 first and prints 175.

The precedence lesson (opens in a new tab) traces how Java reads a line like this, step by step.

Next step

Check your understanding

8 quick questions