01 / 21
>_

Expressions &
Output()

Master how Java evaluates expressions and prints results with System.out.print and System.out.println.

AP Computer Science A

What Is an Expression?

An expression is any valid combination of variables, values, operators, and method calls that Java evaluates to produce a single result. Every expression has a type and a value.

Arithmetic
\( 3 + 5 \times 2 \) evaluates to an int
String
"Hello" + " World" evaluates to a String
Boolean
\( 5 > 3 \) evaluates to true

Anatomy of System.out

Java provides two primary methods for printing to the console. Both belong to the System.out object, which represents the standard output stream.

System.out.println(x) Prints x then moves to next line System.out.print(x) Prints x, cursor stays on same line Hello World HelloWorld

Types of Expressions in Java

Java expressions fall into several categories. The AP exam focuses on arithmetic, String, and boolean expressions, each following specific evaluation rules.

1
Literals: Fixed values like 42, "Hi", or true
2
Arithmetic: Use operators +, -, *, /, % to compute numeric results
3
String concatenation: The + operator joins Strings together
4
Method calls: Expressions like Math.abs(-5) return a value

Operator Precedence Rules

Java evaluates expressions using a strict order of operations, similar to math. Higher-precedence operators are evaluated first, left to right within the same level.

( ) Parentheses Highest * / % Multiply, Divide, Mod + - Add, Subtract, Concat = Assignment (evaluated last) Lowest

Integer Division & Modulo

When both operands are int values, Java performs integer division: it truncates (drops) the decimal portion. The % (modulo) operator returns the remainder.

int a = 17 / 5; // a = 3, not 3.4 int b = 17 % 5; // b = 2 (remainder) double c = 17.0 / 5; // c = 3.4 (double division)

String Concatenation with +

The + operator is overloaded in Java. When at least one operand is a String, it concatenates. Evaluation proceeds left to right, which is crucial for mixed expressions.

System.out.println("Score: " + 3 + 4); // Output: Score: 34 System.out.println(3 + 4 + " points"); // Output: 7 points

print vs println

System.out.print System.out.println
Newline after output? No Yes
Empty call allowed? No (compile error) Yes (prints blank line)
Use case Building output on one line Printing complete lines
Typical AP usage Loops printing horizontally Most standard output

How to Trace Output

Follow these steps to determine what a print statement produces:

1
Identify the expression inside the parentheses of print/println
2
Evaluate parentheses first, then *, /, % before +, -
3
Process left to right: check if + is arithmetic or concatenation at each step
4
Check int vs double: will division truncate or produce a decimal?
5
Determine newline: is it print (stays) or println (moves down)?

Example 1: Basic Arithmetic

int x = 10; int y = 3; System.out.println(x + y); System.out.println(x - y); System.out.println(x * y); System.out.println(x / y); System.out.println(x % y);
Console Output
13 7 30 3 1

Example 2: Mixed Concatenation

int a = 5; int b = 10; System.out.println("Sum: " + a + b); System.out.println("Sum: " + (a + b)); System.out.println(a + b + " is the sum");
Console Output
Sum: 510 Sum: 15 15 is the sum

Example 3: print vs println

System.out.print("A"); System.out.print("B"); System.out.println("C"); System.out.print("D"); System.out.println(); System.out.println("E");
Console Output
ABC D E

Escape Sequences

Java uses backslash \ as an escape character inside Strings to represent special characters that cannot be typed directly.

\n
Newline: moves cursor to the next line
\\
Backslash: prints a literal \
\"
Quote: prints a literal " inside a String
System.out.println("Line1\nLine2"); System.out.println("She said \"Hi\""); System.out.println("Path: C:\\Users");
Console Output
Line1 Line2 She said "Hi" Path: C:\Users

Casting & Type Promotion

Java automatically promotes int to double in mixed expressions. You can also explicitly cast to control the type of a result.

int x = 7; int y = 2; System.out.println(x / y); // 3 (int division) System.out.println((double) x / y); // 3.5 (cast x to double) System.out.println((double)(x / y)); // 3.0 (int division first)

Common Mistakes

✕ Expecting decimal from int / int
Writing 7 / 2 and expecting 3.5: integer division yields 3. Cast to double first.
✕ Left-to-right concatenation trap
Writing "Value: " + 3 + 4 produces "Value: 34", not "Value: 7". Use parentheses: (3 + 4).
✕ Forgetting println adds \n
Using println inside a loop when you want output on one line. Use print instead.

Quick Reference Table

Expression Result Type
7 / 2 3 int
7.0 / 2 3.5 double
7 % 2 1 int
"A" + 1 + 2 "A12" String
1 + 2 + "A" "3A" String
"A" + (1 + 2) "A3" String
(double)(7 / 2) 3.0 double
(double) 7 / 2 3.5 double

Question 1 of 5

What is the output of the following statement?
System.out.println(10 / 3);

Question 2 of 5

What is the output of the following code?
System.out.println("Result: " + 2 + 3);

Question 3 of 5

What is the output of the following code?

System.out.print("X"); System.out.print("Y"); System.out.println("Z"); System.out.println("W");

Question 4 of 5

What is the value of result after this line executes?
double result = (double)(9 / 2);

Question 5 of 5

What does this statement print?
System.out.println(5 + 3 + " apples and " + 2 + 1 + " oranges");