TOPIC 1.3 · STUDY

Expressions and Output

Java writes to the console with System.out.print and System.out.println. String literals can include escape sequences such as \" and \n. Arithmetic uses +, -, *, /, and %; integer division truncates toward zero, and *, /, and % run before + and -.

1.3.A

print and println

System.out.print writes text and leaves the cursor where it stopped. The next print or println continues on that same line. System.out.println writes text and then ends the line, so the next output starts on a new line. Neither call inserts spaces on its own.

System.out.print("Tide");
System.out.print("Surge");
System.out.println();
System.out.println("Calm");
System.out.print("Bay");

The first two prints produce TideSurge on one line. The empty println() ends that line without adding more characters, which leaves a blank line before Calm. The last println ends the Calm line, so Bay begins on the line after that.

Two small consoles side by side. Left: print then print keeps TideSurge on one line. Right: println then print puts Tide on the first line and Surge on the second.

Same words, different methods: print keeps the cursor on the line; println moves it to the next line.

Call Writes text? Ends the line?
System.out.print(...) Yes No
System.out.println(...) Yes Yes
System.out.println() No Yes

Key points

  • print continues on the same line; println ends the line after writing.
  • An empty println() creates a blank line.
  • Spaces appear only if you put them in the string or expression you print.

1.3.B

String literals and escapes

A string literal is text wrapped in double quotes. The quotes that open and close the literal are delimiters in the source. They are not part of the characters sent to the console.

Some characters cannot sit inside a string as plain text. An escape sequence starts with a backslash. \" puts a quotation mark into the output. \n starts a new line inside the printed text.

System.out.print("Gate: \"open\"\nClear");

That statement displays Gate: "open" on the first line and Clear on the second. The \" escapes become ordinary quote characters. The \n escape is a line break, not the two characters backslash and n.

In the source On the console
\" A quotation mark character
\n A new line
Ordinary letters Those same letters

Key points

  • String delimiters surround the literal; they are not printed.
  • \" displays a quotation mark inside the string.
  • \n inserts a newline inside the string.

1.3.C

Arithmetic expressions

Java evaluates +, -, *, /, and % in expressions. Addition and subtraction are + and -. Multiplication is *. Division is /. The remainder operator % gives what is left after integer division. You can print the result of an expression directly.

When both operands of / or % are integers, the result is an integer. Integer division truncates toward zero: any fractional part is dropped. So 17 / 5 is 3, and 17 % 5 is 2.

Operators Precedence Association
* / % Higher Left to right
+ - Lower Left to right

Operators at the same level still run left to right. Parentheses change the order when you need a different grouping.

System.out.print(9 + 14 / 4 * 2);
System.out.println();
System.out.print((9 + 14) / 4 * 2);

In the first expression, / and * run before +: 14 / 4 is the integer 3, then 3 * 2 is 6, then 9 + 6 is 15. In the second expression, parentheses force 9 + 14 first, so 23 / 4 is 5 and 5 * 2 is 10. The empty println separates the two results on the console.

Key points

  • The five arithmetic operators are +, -, *, /, and %.
  • Integer / and % drop the fractional part; division truncates toward zero.
  • * / % have higher precedence than + - and associate left to right.
  • Parentheses override the default order.

Do not treat integer division like a calculator that keeps decimals. 14 / 4 is 3, not 3.5. When * / % share a stretch of an expression, evaluate them left to right before you apply + or -.

Practice this topic