Before writing code, you need to understand the machine running it and the language you're writing in. This unit covers the foundations.
Hardware is the physical stuff you can touch. Every computer — from a phone to a server rack — has these core components working together.
The "brain" of the computer. It executes instructions from programs by performing arithmetic, logic, and control operations. Modern CPUs have multiple cores, allowing them to run instructions in parallel.
The main circuit board that connects all components together. The CPU, RAM, storage, and peripherals all plug into the motherboard. Think of it as the nervous system connecting every organ.
Electrical pathways that carry data between components. The data bus moves data, the address bus tells where to send it, and the control bus coordinates the operation. Wider busses = more data moved per cycle.
Long-term, non-volatile storage — data persists when power is off. HDDs use spinning magnetic platters; SSDs use flash memory chips (faster, no moving parts).
Fast, volatile memory used for data the CPU needs right now. Programs and their variables live in RAM while running. When you turn off the computer, RAM is wiped clean.
Non-volatile memory with permanent instructions, like the BIOS/UEFI firmware that starts the boot process. It cannot be easily modified — that's the "read-only" part.
| Volatile | Non-Volatile | |
|---|---|---|
| Data on power off? | Lost | Retained |
| Speed | Very fast | Slower |
| Example | RAM | Hard drive, SSD, ROM |
| Purpose | Active working memory | Permanent storage |
External devices connected to the computer. Input peripherals send data in (keyboard, mouse, microphone). Output peripherals display results (monitor, speakers, printer). Some are both — like a touchscreen.
Software is the set of instructions that tells hardware what to do. Programming languages are how humans write those instructions.
Close to the hardware. Machine code (binary 1s and 0s) is the lowest — it's what the CPU actually executes. Assembly language is one step up, using short mnemonics like MOV, ADD, JMP. Fast but hard for humans to read and write.
Closer to human language. Java, Python, C++ — these are high-level. They must be translated into machine code before the CPU can run them. Much easier to write, read, and maintain. Java is the language used on the AP CSA exam.
The smallest unit of data in computing. A bit is a single 0 or 1. Everything in a computer — numbers, text, images, your Java programs — is ultimately represented as sequences of bits. Why binary? Because electronic circuits have two states: on (1) and off (0).
We normally count in base 10 (decimal) because we have 10 fingers. Computers use base 2 (binary). Programmers also use base 8 (octal) and base 16 (hexadecimal) as shorthand for binary.
| Base | Name | Digits | Example |
|---|---|---|---|
| 2 | Binary | 0, 1 | 1010 = 10 in decimal |
| 8 | Octal | 0 - 7 | 12 = 10 in decimal |
| 10 | Decimal | 0 - 9 | 10 |
| 16 | Hexadecimal | 0 - 9, A - F | A = 10 in decimal |
Divide by 2 repeatedly, record the remainders, then read them bottom to top.
13 / 2 = 6 remainder 1
6 / 2 = 3 remainder 0
3 / 2 = 1 remainder 1
1 / 2 = 0 remainder 1
↑ read bottom-to-top
13 in decimal = 1101 in binaryMultiply each digit by its place value (powers of 2) and add them up.
Position: 3 2 1 0
Binary: 1 1 0 1
Value: 2³ 2² 2¹ 2⁰
= 8 + 4 + 0 + 1 = 13int decimal = 13; // base 10 (normal)
int binary = 0b1101; // base 2 (prefix 0b)
int octal = 015; // base 8 (prefix 0)
int hexadecimal = 0xD; // base 16 (prefix 0x)
// All four variables hold the same value: 13
System.out.println(decimal); // 13
System.out.println(binary); // 13
System.out.println(octal); // 13
System.out.println(hexadecimal); // 13Memory is measured in bits and bytes. Everything scales by powers of 2 (in computer science) or powers of 10 (in marketing — watch out for that on the exam).
| Unit | Size | Approx. Real-World |
|---|---|---|
| 1 Bit | 0 or 1 | A single yes/no answer |
| 1 Byte | 8 bits | A single character (like 'A') |
| 1 Kilobyte (KB) | 1,024 bytes | A short email |
| 1 Megabyte (MB) | 1,024 KB | A photo or short song |
| 1 Gigabyte (GB) | 1,024 MB | ~250 songs or a short movie |
| 1 Terabyte (TB) | 1,024 GB | ~500 hours of video |
Every Java program needs at minimum: a class and a main method. The file name must match the class name exactly (including capitalization) and end in .java.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, AP CSA!");
}
}| Part | Meaning |
|---|---|
public | Accessible from anywhere |
class HelloWorld | Defines a class named HelloWorld (must match file name) |
{ } | Curly braces define the body of the class / method |
public static void main(String[] args) | The entry point — Java starts running here |
System.out.println(...) | Prints text to the console with a newline |
; | Every statement in Java ends with a semicolon |
Comments are notes for humans. The compiler ignores them completely. Use them to explain why code exists, not what it does (the code itself should show that).
// This is a single-line comment.
// Everything after // on this line is ignored.
/*
This is a multi-line comment.
It can span as many lines as you need.
Great for longer explanations.
*/
/**
* This is a Javadoc comment.
* Used to generate documentation for classes and methods.
* You'll see these in the AP Quick Reference sheet.
*/
public class CommentDemo {
public static void main(String[] args) {
System.out.println("Comments are ignored by the compiler");
// System.out.println("This line won't run — it's commented out");
}
}| Type | Syntax | Use Case |
|---|---|---|
| Single-line | // comment | Short notes, disabling one line |
| Multi-line | /* comment */ | Longer explanations, block-disable code |
| Javadoc | /** comment */ | API documentation for classes/methods |
When code doesn't work, the error falls into one of three categories. Knowing which type you're dealing with tells you where andhow to fix it.
The code violates Java's grammar rules. The compiler catches these before the program runs. You literally cannot run the program until these are fixed.
System.out.println("Hello")
// ^ missing ;
// Compiler error:
// ';' expectedThe code compiles fine, but crashes while running. The program terminates with an exception message. Common causes: dividing by zero, accessing a null object, array index out of bounds.
int x = 10;
int y = 0;
System.out.println(x / y);
// Compiles fine, but crashes:
// ArithmeticException: / by zeroThe code compiles and runs without crashing, but produces the wrong result. No error message — the program just does the wrong thing. These are the hardest to find.
// Goal: average of 3 scores
int a = 80, b = 90, c = 100;
double avg = a + b + c / 3;
System.out.println(avg);
// Prints 203.0 — not 90.0!
// Bug: division happens before
// addition (operator precedence).
// Fix: (a + b + c) / 3.0High-level code must be translated into something the machine understands. There are two main strategies:
| Compiler | Interpreter | |
|---|---|---|
| How it works | Translates the entire program at once, producing an output file | Translates and runs the program one line at a time |
| Error detection | Reports all syntax errors before running | Stops at the first error it encounters |
| Speed | Slower to compile, faster to run | No compile step, but slower execution |
| Output | A separate executable or bytecode file | No separate file — runs directly |
| Examples | Java (javac), C, C++ | Python, JavaScript |
Java uses both. The javac compiler translates your .java source into bytecode (.class files). Then the JVM interprets (or JIT-compiles) that bytecode at runtime.
This is Java's superpower: "Write once, run anywhere."
Bytecode is not machine code — it's an intermediate format that any JVM can understand. So the same .class file runs on Windows, macOS, or Linux without recompiling. Each platform just needs its own JVM installed.
$ javac HelloWorld.java # Step 1: compile → creates HelloWorld.class
$ java HelloWorld # Step 2: run bytecode on the JVM
Hello, AP CSA! # OutputBoth methods belong to the PrintStream class (accessed via System.out). The only difference: does the cursor move to the next line after printing?
| Method | Newline after? | Usage |
|---|---|---|
System.out.print() | No | Keeps cursor on the same line |
System.out.println() | Yes | Moves cursor to the next line after printing |
public class PrintDemo {
public static void main(String[] args) {
// println moves to the next line after each call
System.out.println("Line 1");
System.out.println("Line 2");
System.out.println(); // prints a blank line
// print stays on the same line
System.out.print("Hello ");
System.out.print("World");
System.out.println("!"); // finishes the line
// Mixing them together
System.out.print("A");
System.out.println("B");
System.out.print("C");
System.out.print("D");
}
}Line 1
Line 2
Hello World!
AB
CDThe AP exam loves asking you to predict the output of code that mixes print and println. Trace through each statement and track where the cursor is. Pay close attention to spaces — they only appear if you explicitly include them in the string.
System.out.print("AP");
System.out.println(" CSA");
System.out.print("Unit ");
System.out.print(1);
System.out.println();
System.out.println("Done!");AP CSA
Unit 1
Done!