# APCS-A Unit 1 Study Guide: Primitive Types
*Prepared for the May 2026 APCS-A Exam*

## Overview
Unit 1 covers **Primitive Types**, which forms the foundation for all Java programming. This unit typically accounts for **2.5-5%** of the multiple-choice questions and appears in most Free Response Questions as fundamental building blocks.

---

## 📋 Topic Checklist
- [ ] Four primitive data types (int, double, boolean, char)
- [ ] Variable declaration and initialization
- [ ] Arithmetic operators and precedence
- [ ] Assignment operators
- [ ] Type casting (implicit and explicit)
- [ ] Input/output operations
- [ ] Common pitfalls and error patterns

---

## 🔢 Primitive Data Types

### 1. **int** (Integer)
- **Range**: -2,147,483,648 to 2,147,483,647
- **Use for**: Counting, indexing, whole number calculations
- **Examples**: `int age = 16;`, `int score = 95;`

### 2. **double** (Double-precision floating-point)
- **Range**: Approximately ±1.7 × 10^308
- **Use for**: Decimal calculations, measurements, averages
- **Examples**: `double gpa = 3.85;`, `double temperature = 98.6;`

### 3. **boolean** (Boolean)
- **Values**: `true` or `false` only
- **Use for**: Flags, conditions, yes/no situations
- **Examples**: `boolean isRaining = true;`, `boolean passed = false;`

### 4. **char** (Character)
- **Range**: 0 to 65,535 (Unicode values)
- **Use for**: Single characters
- **Examples**: `char grade = 'A';`, `char symbol = '$';`

> ⚠️ **COMMON MISTAKE**: Using double quotes for char (`char letter = "A";` ❌) instead of single quotes (`char letter = 'A';` ✅)

---

## 🔧 Variable Operations

### Declaration and Initialization
```java
// Declaration only
int count;
double price;

// Declaration with initialization (PREFERRED)
int students = 25;
double tax = 0.08;
boolean ready = false;
char initial = 'K';
```

### Assignment Operators
- `=` : Assignment
- `+=` : Add and assign (`x += 5` same as `x = x + 5`)
- `-=` : Subtract and assign
- `*=` : Multiply and assign
- `/=` : Divide and assign
- `%=` : Modulus and assign

---

## ➕ Arithmetic Operations

### Basic Operators (in order of precedence)
1. **Parentheses** `()`
2. **Unary operators** `++`, `--`, `+`, `-`
3. **Multiplication, Division, Modulus** `*`, `/`, `%`
4. **Addition, Subtraction** `+`, `-`

### 🚨 CRITICAL PROBLEM AREA: Integer Division
```java
int result1 = 7 / 2;        // Result: 3 (NOT 3.5!)
double result2 = 7 / 2;     // Result: 3.0 (still integer division)
double result3 = 7.0 / 2;   // Result: 3.5 (floating-point division)
double result4 = 7 / 2.0;   // Result: 3.5 (floating-point division)
```

### Modulus Operator (%)
- Returns the **remainder** after division
- `17 % 5 = 2` (17 ÷ 5 = 3 remainder 2)
- `20 % 4 = 0` (20 ÷ 4 = 5 remainder 0)
- **With negatives**: Result has same sign as the dividend
  - `-7 % 3 = -1`
  - `7 % -3 = 1`

### Increment/Decrement Operators
```java
int x = 5;

// Pre-increment: increment first, then use value
int a = ++x;  // x becomes 6, a gets 6

// Post-increment: use value first, then increment
int b = x++;  // b gets 6, x becomes 7
```

---

## 🔄 Type Casting

### Implicit Casting (Automatic - Widening)
Java automatically converts from smaller to larger data types:
```java
int i = 100;
double d = i;        // int → double (automatic)
char c = 'A';
int ascii = c;       // char → int (automatic)
```

**Widening Order**: `byte` → `short` → `int` → `long` → `float` → `double`

### Explicit Casting (Manual - Narrowing)
Must use cast operator when converting from larger to smaller:
```java
double d = 3.14159;
int i = (int) d;     // Result: 3 (decimal part truncated!)

int large = 300;
byte small = (byte) large;  // Possible overflow!
```

### 🚨 MAJOR PITFALL: Casting in Expressions
```java
int a = 7, b = 2;

double result1 = (double) a / b;      // 7.0 / 2 = 3.5 ✅
double result2 = (double) (a / b);    // (double) 3 = 3.0 ❌
```

---

## 📊 Operator Precedence Problems

### Example 1: Basic Precedence
```java
int result = 2 + 3 * 4;  // Result: 14 (not 20!)
// Calculation: 2 + (3 * 4) = 2 + 12 = 14
```

### Example 2: Left-to-Right Evaluation
```java
int result = 12 / 3 * 2;  // Result: 8
// Calculation: (12 / 3) * 2 = 4 * 2 = 8
```

### Example 3: Mixed Types
```java
double result = 5 / 2 * 2.0;  // Result: 4.0
// Calculation: (5 / 2) * 2.0 = 2 * 2.0 = 4.0
```

---

## 🎯 Common Exam Patterns

### Pattern 1: Trace Code Execution
Given variables, trace through operations step by step.

### Pattern 2: Predict Output
Determine what will be printed given specific code.

### Pattern 3: Identify Errors
Find compilation or logical errors in code snippets.

### Pattern 4: Choose Equivalent Expressions
Select expressions that produce the same result.

---

## 🚨 High-Frequency Mistake Categories

### 1. Integer Division Confusion
- **Problem**: `int average = (80 + 90 + 70) / 3;` gives 80, not 80.0
- **Solution**: Use `double` or cast: `double average = (80 + 90 + 70) / 3.0;`

### 2. Operator Precedence Errors
- **Problem**: `2 + 3 * 4` evaluated as `(2 + 3) * 4`
- **Remember**: Multiplication/division before addition/subtraction

### 3. Type Casting Data Loss
- **Problem**: `(int) 3.9` gives `3`, not `4`
- **Remember**: Casting truncates, doesn't round

### 4. Assignment vs. Equality
- **Problem**: Using `=` instead of `==` in conditions
- **Remember**: `=` assigns, `==` compares

### 5. Uninitialized Variables
- **Problem**: Using variables before assigning values
- **Solution**: Always initialize variables when declared

---

## 📝 Practice Problems

### Problem Set A: Basic Operations
1. `int x = 15; int y = 4; int z = x / y * 2;` What is `z`?
2. `double a = 7; double b = 3; double c = a / b;` What is `c`?
3. `int p = 17; int q = 5; int r = p % q;` What is `r`?

### Problem Set B: Type Casting
1. `double d = 5.8; int i = (int) d;` What is `i`?
2. `char c = 'B'; int ascii = c;` What is `ascii`?
3. `int x = 7; int y = 2; double z = (double) x / y;` What is `z`?

### Problem Set C: Complex Expressions
1. `int result = 3 + 4 * 2 - 1;` What is `result`?
2. `double val = 10 / 3 * 1.0;` What is `val`?
3. `int a = 5; int b = ++a * 2;` What are `a` and `b`?

### Solutions
**Set A**: 1) 6, 2) 2.333..., 3) 2  
**Set B**: 1) 5, 2) 66, 3) 3.5  
**Set C**: 1) 10, 2) 3.0, 3) a=6, b=12

---

## 🎓 Exam Strategy Tips

### For Multiple Choice:
1. **Trace carefully**: Work through each step methodically
2. **Watch for traps**: Look for integer division and precedence issues
3. **Check data types**: Pay attention to `int` vs `double`
4. **Test edge cases**: Consider negative numbers, zero, and overflow

### For Free Response:
1. **Initialize variables**: Always give variables initial values
2. **Use meaningful names**: Make your code readable
3. **Comment tricky parts**: Explain complex calculations
4. **Test your logic**: Mentally trace through your code

### Time Management:
- **Multiple Choice**: ~1 minute per question
- **Don't get stuck**: Mark difficult questions and return later
- **Double-check**: Verify operator precedence and type casting

---

## 📚 Additional Resources

### CollegeBoard Resources:
- AP Computer Science A Course Description
- AP Classroom practice questions
- Previous FRQ solutions

### Practice Websites:
- CodingBat (Java exercises)
- AP Central (official practice exams)
- Barron's/Princeton Review prep books

---

## ✅ Pre-Test Checklist

**The day before your test, ensure you can:**
- [ ] Declare and initialize all four primitive types
- [ ] Perform calculations with proper operator precedence
- [ ] Cast between types without data loss confusion
- [ ] Identify integer vs. floating-point division
- [ ] Trace through complex expressions step-by-step
- [ ] Spot common errors in code snippets

**Good luck on your Unit 1 test! Remember: practice makes perfect!** 🎯