AP Computer Science A
One-page, printable cheatsheet
Cheatsheet visualization
Find gaps with guided practice
Guided practice grid visualization
Table of Contents

💻ap computer science a review

1.3 Expressions and Assignment Statements

Verified for the 2025 AP Computer Science A examCitation:

Arithmetic Expressions

What is a Literal?

A literal is the source code representation of a fixed value. These are values that appear directly in your code.

Examples:

  • 5 - integer literal
  • 3.14 - double literal
  • 'a' - character literal
  • "Hello" - string literal

Arithmetic Expression Types

Arithmetic expressions in Java can be of type int or double:

TypeExampleDescription
int5 + 3Whole numbers without decimal points
double5.2 - 1.7Numbers that can have decimal points

Arithmetic Operators

Java provides five basic arithmetic operators:

| Operator | Description         | Example  |
|----------|---------------------|----------|
| +        | Addition            | a + b    |
| -        | Subtraction         | a - b    |

| *        | Multiplication      | a * b    |
| /        | Division            | a / b    |
| %        | Modulus (remainder) | a % b    |

Type Conversion Rules

Understanding how types interact in expressions is crucial:

  • An operation with two int values will produce an int result
int result = 7 / 2;  // result is 3, not 3.5
  • An operation involving at least one double value will produce a double result
double result = 7 / 2.0;  // result is 3.5
  • Integer division truncates (cuts off) any decimal portion
int x = 5 / 2;  // x is 2, not 2.5

Division by Zero

Be careful with division operations:

  • Dividing an int by zero results in an ArithmeticException
int badResult = 5 / 0;  // Causes ArithmeticException
  • Dividing a double by zero doesn't throw an exception but gives special values:
double result1 = 5.0 / 0.0;  // result is Infinity
double result2 = -5.0 / 0.0; // result is -Infinity
double result3 = 0.0 / 0.0;  // result is NaN (Not a Number)

Compound Expressions

Operator Precedence

When an expression contains multiple operators, Java follows these precedence rules:

  1. Expressions in parentheses ()
  2. Unary operators (+, -, !)
  3. Multiplication, division, modulus (*, /, %)
  4. Addition, subtraction (+, -)

Example:

int result = 2 + 3 * 4;  // result is 14, not 20
int result2 = (2 + 3) * 4;  // result is 20

Associativity

When operators of the same precedence appear, Java evaluates them according to their associativity:

  • Most binary operators are left-associative (evaluated left to right)
int a = 10 - 5 - 2;  // Evaluated as (10 - 5) - 2 = 3

  • Assignment operators are right-associative (evaluated right to left)
int a, b, c;
a = b = c = 5;  // Evaluated as a = (b = (c = 5))

Assignment Statements

Basic Assignment

The assignment operator (=) stores the value of the right-side expression into the variable on the left:

int score = 95;  // Assigns the value 95 to the variable score

Compound Assignment Operators

Java provides shorthand operators that combine arithmetic and assignment:

| Operator | Example  | Equivalent to   |
|----------|----------|-----------------|
| +=       | x += 5   | x = x + 5       |
| -=       | x -= 5   | x = x - 5       |

| *=       | x *= 5   | x = x * 5       |
| /=       | x /= 5   | x = x / 5       |
| %=       | x %= 5   | x = x % 5       |

Example:

int count = 10;
count += 5;  // count is now 15
count *= 2;  // count is now 30

Assignment and Type Conversion

When assigning values of different types, Java performs automatic type conversion if the assignment is allowed:

int i = 5;
double d = i;  // Implicit conversion from int to double (allowed)

double pi = 3.14159;
int approx = pi;  // This will cause a compilation error - explicit cast needed

int approx = (int)pi;  // Explicit cast - truncates to 3

Common Pitfalls and Tips

Integer Division Surprise

One of the most common mistakes is expecting decimal results from integer division:

// What's wrong with this average calculation?
int score1 = 95;
int score2 = 98;
double average = (score1 + score2) / 2;  // Will be 96.0, not 96.5

// Correct approach:
double average = (score1 + score2) / 2.0;  // Will be 96.5

Increment and Decrement Operators

The ++ and -- operators increase or decrease a variable by 1:

int count = 5;
count++;  // count is now 6
count--;  // count is back to 5

These can be used as prefix or postfix, with different behaviors in expressions:

int a = 5;
int b = ++a;  // a is incremented to 6, then b is assigned 6
// Now a is 6, b is 6

int c = 5;
int d = c++;  // d is assigned 5, then c is incremented to 6
// Now c is 6, d is 5

Order of Evaluation

Be careful with expressions that modify the same variable multiple times:

int x = 5;
int result = x * (x++);  // Behavior can be confusing, avoid this pattern

Remember that understanding expressions and assignments is fundamental to programming in Java. Pay close attention to the types involved in your expressions and be mindful of operator precedence when writing compound expressions.

Key Terms to Review (6)

Assignment operator: The assignment operator is used to assign a value to a variable. It is represented by the equals sign (=) and it assigns the value on the right side of the equals sign to the variable on the left side.
Double: In Java, Double is both a wrapper class for primitive type double and also represents real numbers with decimal points. It provides useful methods for performing mathematical operations on these numbers.
Int: The int is a primitive data type in Java used to store whole numbers without decimal points. It has a fixed size and represents integer values within a specific range.
Modulo Operator: The modulo operator, represented by the symbol %, returns the remainder of a division operation. It is used to determine if a number is divisible by another number.
Order of Operations: The order in which mathematical operations should be performed to obtain the correct result. It follows the acronym PEMDAS, which stands for Parentheses, Exponents, Multiplication and Division (from left to right), and Addition and Subtraction (from left to right).
System.out.println: System.out.println is a Java statement used to display output on the console. It prints the specified message or value and adds a new line character at the end.