Expressions & Output

Printing Output

Two ways to print in Java:

  • System.out.println(value) — prints, then moves to a new line.
  • System.out.print(value) — prints, and stays on the same line.
System.out.print("Score: ");
System.out.println(24);
// Output: Score: 24

Joining Text and Values With +

+ doesn't just add numbers — between a String and anything else, it joins them into one longer piece of text instead:

int score = 24;
System.out.println("Score: " + score); // Score: 24

Every worked example so far has been doing this already. The catch: + still runs left to right, so where the text shows up in the expression changes the result:

System.out.println("a" + 1 + 2); // a12 — "a"+1 makes "a1" (text), then +2 makes "a12" (text)
System.out.println(1 + 2 + "a"); // 3a  — 1+2 makes 3 (math) first, then +"a" makes "3a" (text)

Once a + touches a String, everything after it becomes text-joining, left to right — there's no going back to math mode later in the same line.

Text inside "" can also contain escape sequences — a backslash followed by a letter, meaning something other than a literal character: \n (new line), \t (tab), \" (a literal quote mark inside the text), and \\ (a literal backslash).

System.out.println("Line one\nLine two"); // prints on two separate lines
System.out.println("She said \"go\"");    // She said "go"

Math Expressions

+ - * / work the same way you'd expect, with one big exception: dividing two whole numbers throws away the decimal part.

System.out.println(7 / 2); // prints 3, not 3.5

This is called truncating division, and it's the single most common source of "wrong answer, no error message" bugs you'll run into. If you want the decimal part, at least one of the two numbers needs to be a double:

System.out.println(7.0 / 2); // prints 3.5

Order of Operations

Same rules as math class: * and / happen before + and -. Parentheses override that.

System.out.println(2 + 3 * 2);   // 8
System.out.println((2 + 3) * 2); // 10

The Remainder Operator (%)

% gives you what's left over after dividing. 5 % 2 is 1, because 2 fits into 5 twice, with 1 left over.

int totalSeconds = 150;
System.out.println(totalSeconds / 60); // 2 — whole minutes
System.out.println(totalSeconds % 60); // 30 — leftover seconds
// 150 seconds = 2 minutes, 30 seconds

% binds as tightly as * and / (so it follows the same order-of-operations rule above). It also behaves a little differently with negative numbers than you might expect: -7 / 2 is -3 (still truncated toward zero, not rounded down), and -7 % 2 is -1 (the remainder keeps the sign of the number being divided).

Common Pitfalls

  • Dividing two whole numbers and expecting a decimal. It truncates instead — check whether at least one side is a double.
  • Dividing by zero. Whole-number division by 0 crashes the program. Decimal division by 0.0 doesn't crash, but gives a nonsense result (Infinity or NaN) — a sign something's wrong upstream.

Key Takeaways

  • print stays on the line; println moves to a new one.
  • Dividing two whole numbers truncates the decimal part instead of rounding.
  • Use *// before +/-, unless parentheses say otherwise.
  • % gives the remainder after division — useful for splitting a total into two units (like seconds into minutes and seconds).

Derived from csawesome-2026: unit-1-using-objects-and-methods/1.03-expressions-and-output.md (CSA 1.3)