Introduction
Once you can store data in variables, you need ways to display results and perform calculations. Topic 1.3 covers how to output information to the screen, work with different types of literal values, and create arithmetic expressions using Java's operators.
Expressions are the building blocks of computation. Understanding how Java evaluates expressions—especially operator precedence and the difference between integer and floating-point division—is crucial for writing correct programs and avoiding subtle bugs.
Output in Java
System.out.print and System.out.println
Java provides two primary methods for displaying information on the computer screen:
System.out.println displays information and then moves the cursor to a new line:
System.out.println("Hello");
System.out.println("World");Output:
Hello
WorldSystem.out.print displays information but keeps the cursor on the same line:
System.out.print("Hello");
System.out.print("World");Output:
HelloWorldCombining both:
System.out.print("First ");
System.out.print("Second ");
System.out.println("Third");
System.out.println("Fourth");Output:
First Second Third
FourthOutputting Variables
You can display variable values by passing them to the output methods:
int score = 95;
System.out.println(score); // 95
double price = 19.99;
System.out.println(price); // 19.99
boolean isValid = true;
System.out.println(isValid); // true
Literals
What is a Literal?
A literal is the code representation of a fixed value. Literals are the actual values you write directly in your code.
Examples of literals:
42 // int literal
3.14 // double literal
true // boolean literal
"Hello" // String literalUsing literals:
int x = 10; // 10 is an int literal
double pi = 3.14159; // 3.14159 is a double literal
boolean flag = false; // false is a boolean literalString Literals
A string literal is a sequence of characters enclosed in double quotes. String literals represent text.
Valid string literals:
"Hello, World!"
"Java Programming"
"123" // This is text, not a number
"" // Empty string (valid)
"true" // This is text, not booleanCommon mistake - Single quotes create characters, not strings:
"A" // String literal
'A' // char literal (different type, outside AP CSA scope)
Escape Sequences
Escape sequences are special sequences of characters that can be included in a string. They start with a backslash \ and have special meaning in Java.
Three escape sequences used in AP Computer Science A:
| Escape Sequence | Meanng | |
| \ " | Double quote | |
| \ \ | Backslash | |
| \n | Newline |
Examples:
System.out.println("He said, \"Hello!\"");
// Output: He said, "Hello!"
System.out.println("Path: C:\\Program Files\\Java");
// Output: Path: C:\Program Files\Java
System.out.println("Line 1\nLine 2\nLine 3");
// Output: Line 1
// Line 2
// Line 3
Newline with println:
System.out.println("First\nSecond");
// Same as:
System.out.println("First");
System.out.println("Second");
Arithmetic Expressions
What is an Arithmetic Expression?
Arithmetic expressions consist of numeric values, variables, and operators. Expressions of type int and double are used to perform calculations.
5 + 3 // Expression using literals
x * 2 // Expression using variable
(a + b) / 2 // Compound expressionArithmetic Operators
Java provides five arithmetic operators:
Note: Java uses * for multiplication (not ×) and / for division (not ÷).
Type of Arithmetic Operations
Critical Rule: The data type of the result depends on the operands.
When both operands are int: An arithmetic operation that uses two int values will evaluate to an int value.
int a = 10;
int b = 3;
int sum = a + b; // 13 (int)
int product = a * b; // 30 (int)When at least one operand is double: An arithmetic operation that uses at least one double value will evaluate to a double value.
double x = 10.0;
int y = 3;
double result = x + y; // 13.0 (double)
int a = 5;
double b = 2.0;
double product = a * b; // 10.0 (double)
Integer Division vs. Floating-Point Division
This is one of the most important concepts in Topic 1.3.
Integer Division (both operands are int): When dividing numeric values that are both int values, the result is only the integer portion of the quotient. The decimal portion is truncated (cut off), not rounded.
int result1 = 7 / 2; // 3 (not 3.5!)
int result2 = 10 / 4; // 2 (not 2.5!)
int result3 = 5 / 3; // 1 (not 1.666...)
int result4 = 9 / 9; // 1
int result5 = 2 / 5; // 0 (not 0.4!)Floating-Point Division (at least one double): When dividing numeric values that use at least one double value, the result is the full quotient with decimal precision.
double result1 = 7.0 / 2; // 3.5
double result2 = 7 / 2.0; // 3.5
double result3 = 7.0 / 2.0; // 3.5
double result4 = 10.0 / 4; // 2.5
double result5 = 5 / 3.0; // 1.666...Critical comparison:
int a = 7 / 2; // 3
double b = 7 / 2; // 3.0 (still integer division!)
double c = 7.0 / 2; // 3.5 (floating-point division)Even though b is a double variable, the expression 7 / 2 uses integer division because both operands are integers. The result 3 is then converted to 3.0 for storage.
The Remainder Operator (%)
The remainder operator % computes the remainder when one number is divided by another.
Formula: gives the remainder when is divided by .
Examples:
int r1 = 17 % 5; // 2 (17 = 5*3 + 2)
int r2 = 20 % 6; // 2 (20 = 6*3 + 2)
int r3 = 10 % 3; // 1 (10 = 3*3 + 1)
int r4 = 15 % 5; // 0 (15 = 5*3 + 0, divides evenly)
int r5 = 7 % 10; // 7 (7 = 10*0 + 7)
Common uses for remainder:
- Check if a number is even:
- Check if a number is odd:
- Extract last digit:
- Cycle through values:
Important: For the AP exam, a is always non-negative, and b is always positive.
Operator Precedence
When an expression contains multiple operators, Java follows operator precedence rules to determine the order of evaluation.
Precedence rules:
- Parentheses ( ) — highest precedence
- Multiplication *, Division /, Remainder % — equal precedence
- Addition +, Subtraction - — lowest precedence, equal to each other
When operators have equal precedence, they are evaluated from left to right.
Examples:
int a = 5 + 3 * 2;
// 3 * 2 is evaluated first: 6
// Then 5 + 6: 11
// Result: 11
int b = 10 - 4 + 2;
// Left to right: 10 - 4 = 6
// Then 6 + 2 = 8
// Result: 8
int c = 20 / 4 * 2;
// Left to right: 20 / 4 = 5
// Then 5 * 2 = 10
// Result: 10
int d = (5 + 3) * 2;
// Parentheses first: 5 + 3 = 8
// Then 8 * 2 = 16
// Result: 16Complex expression:
int result = 10 + 6 / 2 * 3 - 4;Step-by-step evaluation:
- (division, higher precedence)
- (multiplication, left to right with division)
- (addition, lower precedence)
- (subtraction, left to right with addition)
- Result:
Using parentheses to change order:
int result = (10 + 6) / (2 * 3) - 4;Step-by-step:
- (parentheses first)
- (parentheses first)
- (integer division)
- Result:
Division by Zero
ArithmeticException: An attempt to divide an integer by the integer zero will result in an ArithmeticException, which causes the program to crash.
int a = 10;
int b = 0;
int result = a / b; // ArithmeticException!Error message:
Exception in thread "main" java.lang.ArithmeticException: / by zeroImportant: This only applies to integer division. Dividing a double by zero produces special values (Infinity, -Infinity, or NaN), which are outside the scope of AP Computer Science A.
Common Mistakes
Mistake 1: Forgetting integer division truncates
// WRONG expectation
int avg = 7 / 2; // Student expects 3.5
System.out.println(avg); // Actually prints 3
// CORRECT if you want 3.5
double avg = 7.0 / 2;
System.out.println(avg); // Prints 3.5Mistake 2: Incorrect operator precedence
// WRONG
int result = 5 + 3 * 2;
// Student thinks: (5 + 3) * 2 = 16
// Actually: 5 + (3 * 2) = 11
// CORRECT - use parentheses
int result = (5 + 3) * 2; // 16Mistake 3: Confusing remainder with division
int a = 17 / 5; // 3 (quotient)
int b = 17 % 5; // 2 (remainder)
// 17 = 5 * 3 + 2
// ^^^^^ ^
// quotient remainderMistake 4: Forgetting escape sequences in strings
// WRONG
System.out.println("He said "Hello""); // Syntax error
// CORRECT
System.out.println("He said \"Hello\"");Mistake 5: Not considering order of operations
// Calculate average of 3 numbers
int a = 10, b = 20, c = 30;
// WRONG
int avg = a + b + c / 3; // 10 + 20 + 10 = 40 (WRONG!)
// CORRECT
int avg = (a + b + c) / 3; // 60 / 3 = 20