Expressions and Operators
Evaluate all Java operator types: arithmetic (+, -, *, /, %), relational (==, !=, <, >, <=, >=), logical (&&, ||, !), and compound assignment (+=, -=, *=, /=, %=). Master integer division as the central skill: 7/2 = 3 (not 3.5) because both operands are int. This is the most tested Java gotcha. Understand modulo behavior with negatives ((-7) % 3 == -1 in Java). Apply operator precedence and distinguish pre-increment (++x, evaluates after) from post-increment (x++, evaluates before). Trace expressions step by step to predict exact values.
A lesson of AP Computer Science A, in Java Foundations, about 10 minutes.
Java Fundamentals: Data Types, Variables, and Operators
By the end of this lesson, you will be able to declare and initialize variables of primitive types, use operators in expressions, and understand Java's type system.
Java Overview
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Java is a statically typed, object-oriented, compiled programming language. Every variable must have a type declared before use; the compiler checks types at compile time.
Program structure:
- Every Java program lives in a class
- Execution starts in the main method
- Statements end with a semicolon
; - Java is case-sensitive:
myVar≠MyVar
Primitive Data Types
Java has 8 primitive types. The AP exam focuses on three:
| Type | Description | Example |
|---|---|---|
int | 32-bit integer | int count = 42; |
double | 64-bit floating-point | double pi = 3.14; |
boolean | true or false | boolean done = false; |
Also used (not heavily tested):
char: single character in single quotes:char grade = 'A';String: technically a class, not a primitive, but used like one
Declaration and initialization:
int x; // declaration only
x = 10; // assignment
int y = 5; // declaration + initialization (preferred)
Integer division: when both operands are int, division truncates (does not round).
int result = 7 / 2; // result is 3, NOT 3.5