print and println
Printing is how a program reports its results. Working engineers live in printed output too, since server logs are millions of print statements, so controlling exactly what appears and where the line breaks fall matters from day one.
Java gives you two printing tools:
| Call | Effect |
|---|---|
System.out.println(x) | prints x, then moves to the next line |
System.out.print(x) | prints x and stays on the same line |
Comments come in two forms:
// single line comment, like Python's # /* multi-line comment, like Python's triple quotes */
And text can be glued to values with +, the way + joined strings in Python:
System.out.println("score: " + 42);
Three prints and a println
Three print calls stay on one line, and the println that follows finally ends it.
public class Main { public static void main(String[] args) { System.out.print("A"); System.out.print("B"); System.out.print("C"); System.out.println("!"); System.out.println("done"); } }
Output
ABC! done
Five statements produce two lines, because only the two println calls end a line. The line break belongs to the printing call, not to the statement.
This matters when output looks mangled. A missing final println leaves the last line without its break, and text from a later part of the program appears glued onto it.
When + adds and when it joins
+ means numeric addition between two numbers and joining when either side is a String. Java evaluates left to right, so the result depends on where the String enters the expression:
System.out.println("sum: " + 1 + 2); // sum: 12 System.out.println("sum: " + (1 + 2)); // sum: 3 System.out.println(1 + 2 + " apples"); // 3 apples
In the first line "sum: " + 1 joins into "sum: 1", and then + 2 joins again, producing 12 where you wanted 3. Parentheses force the addition to happen first, while both sides are still numbers.
The third line works without parentheses because the two numbers meet each other before the text arrives. Left-to-right evaluation is the whole rule, and every case follows from it.
This bites real programs constantly, so wrap arithmetic in parentheses whenever it shares a line with text.
The same operator, three results
Three lines, three different behaviors from one operator.
public class Main { public static void main(String[] args) { System.out.println("sum: " + 1 + 2); System.out.println("sum: " + (1 + 2)); System.out.println(1 + 2 + " apples"); } }
Output
sum: 12 sum: 3 3 apples
The first line joins twice and prints 12, which is the digits 1 and 2 side by side rather than any kind of sum. The second forces the arithmetic with parentheses. The third adds 1 and 2 while both are still numbers, then joins the result onto the text.
Notice how quiet the first bug is. No error appears, and the output even looks plausible, which is exactly why this one reaches production.
Escape sequences
Inside a string, a backslash starts an escape sequence, a stand-in for a character you cannot type directly:
| Escape | Meaning |
|---|---|
\n | new line |
\t | tab |
\" | a literal double quote |
\\ | a literal backslash |
So System.out.println("a\nb") prints a and b on separate lines, exactly like Python's \n.
The escape for a double quote exists because a bare quote would end the string early. The same reasoning explains the doubled backslash: since a single one starts an escape, saying "I mean an actual backslash" takes two.
One println containing \n can therefore produce several lines, which is often tidier than several separate calls.
Reading a tab escape
System.out.print("x\ty") produces x and y separated by a tab, with no new line afterwards.
\t is the tab escape, so a single tab character appears between the two letters. Because the call is print rather than println, the cursor stays on the same line, and whatever prints next continues right after the y.
A tab is one character, not a fixed number of spaces. It advances to the next tab stop, which is why tab-separated output lines up in columns even when the values have different lengths, and why a long value can push its column over.
Two labeled lines from one println
\t inserts a tab and \n starts a new line, and both can live inside a single string.
public class Main { public static void main(String[] args) { System.out.println("Name:\tGrace\nRole:\tEngineer"); } }
Output
Name: Grace Role: Engineer
The whole output is one string, "Name:\tGrace\nRole:\tEngineer", and the println adds the final line break after Engineer.
Two separate println calls would produce identical output. Which version to prefer is a readability question: a single string keeps a small fixed block together, while separate calls are easier to edit when the lines are built from variables.