Printing with println and print
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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
Variables
None yet.
Output
Canteen bill
Line 3println prints the title, then moves the cursor to the next line.
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
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"); }}Name Marks Asha 92 Ravi 85 Teacher said, "Well done" Saved in C:\java\marks.txt
What this code does
\tis a tab: it jumps to the next column, like the Tab key, so the marks line up underMarks.\nstarts 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 inC:\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
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)); }}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 + sciencethe text comes first, so90and85are joined as text:9085. (maths + science)is in brackets, so Java adds 90 + 85 first and prints175.
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