AP CSA Unit 1 | Data & Variables

Primitive types, expressions, assignment, casting, Strings, and Math methods.

Unit 1: Data & Variables

This unit builds the foundation: how Java stores values, evaluates expressions, and prints results. Students must be fluent with integer division, modulus, assignment, casting, and String concatenation.

intdoublebooleanStringMathcasting

AP habit: trace values by hand before trusting your eyes. Most AP mistakes are loop bounds, type conversion, and reference mistakes.

Common AP CSA Unit 1 Mistakes

Why this comes first: AP CSA questions often ask students to predict what code does, not just write new code. These examples target the mistakes that show up again and again with primitive types, expressions, assignment, casting, and Strings.

Student move: Before opening each answer, trace the variables by hand and write the expected output.

Mistake #1: Assignment Is Not Algebra

In Java, = means “store the value on the right into the variable on the left.” It does not mean both sides are permanently equal.

public class Main {
    public static void main(String[] args) {
        int x = 5;
        x = x + 3;
        System.out.println(x);
    }
}
Show explanation

Output: 8

Java evaluates the right side first. The old value of x is 5, so x + 3 becomes 8. Then 8 is stored back into x.

Mistake #2: Integer Division Truncates

If both operands are int values, Java gives an int result and cuts off the decimal part.

public class Main {
    public static void main(String[] args) {
        System.out.println(7 / 2);
        System.out.println(7 / 2.0);
    }
}
Show explanation

Output:

3
3.5

7 / 2 uses integer division. 7 / 2.0 uses floating-point division because one operand is a double.

Mistake #3: Remainder Is Not Percent

The % operator gives the remainder after division. It is not a percent sign.

public class Main {
    public static void main(String[] args) {
        System.out.println(17 % 5);
        System.out.println(20 % 4);
        System.out.println(7 % 2);
    }
}
Show explanation

Output:

2
0
1

Remainder is useful for odd/even checks, last-digit checks, and repeating patterns.

Mistake #4: String Concatenation Changes +

Before Java reaches a String, + does math. Once a String is involved, + joins text.

public class Main {
    public static void main(String[] args) {
        System.out.println(2 + 3 + " meters");
        System.out.println("meters: " + 2 + 3);
        System.out.println("meters: " + (2 + 3));
    }
}
Show explanation

Output:

5 meters
meters: 23
meters: 5

Parentheses force the arithmetic to happen before concatenation.

Mistake #5: Primitive Types vs. Object Types

int, double, and boolean are primitive types. String is an object type.

public class Main {
    public static void main(String[] args) {
        int age = 16;
        double height = 1.75;
        boolean passed = true;
        String name = "Emma";

        System.out.println(name.length());
    }
}
Show explanation

String values have methods, such as length(), substring(), and equals(). Primitive values do not work the same way.

Mistake #6: Casting Truncates

Casting a double to an int cuts off the decimal part. It does not round.

public class Main {
    public static void main(String[] args) {
        double value = 9.8;
        int whole = (int) value;
        System.out.println(whole);
    }
}
Show explanation

Output: 9

If you want rounding, casting is not enough. Casting only keeps the whole-number part.

Mistake #7: Order of Operations Still Matters

Java follows standard operator precedence. Multiplication, division, and modulus happen before addition and subtraction.

public class Main {
    public static void main(String[] args) {
        int answer = 2 + 3 * 4;
        System.out.println(answer);
    }
}
Show explanation

Output: 14

3 * 4 happens first, then 2 is added.

Mistake #8: Compound Assignment Updates in Order

Java executes statements one at a time from top to bottom.

public class Main {
    public static void main(String[] args) {
        int score = 90;
        score += 5;
        score *= 2;
        System.out.println(score);
    }
}
Show explanation

Output: 190

First score becomes 95. Then 95 is doubled to 190.

Mistake #9: Primitive Variables Store Separate Copies

Copying an int value into another variable does not link the variables together.

public class Main {
    public static void main(String[] args) {
        int x = 4;
        int y = x;
        y = y + 3;
        System.out.println(x);
        System.out.println(y);
    }
}
Show explanation

Output:

4
7

y receives a copy of the value 4. Changing y later does not change x.

Mistake #10: Mixed int and double Arithmetic

If either operand is a double, Java promotes the calculation to a decimal result.

public class Main {
    public static void main(String[] args) {
        int a = 5;
        double b = 2;
        System.out.println(a / b);
    }
}
Show explanation

Output: 2.5

Because b is a double, the division is floating-point division.

Unit 1 AP Exam Challenge

This combines primitive types, assignment, mixed arithmetic, and String concatenation.

public class Main {
    public static void main(String[] args) {
        int x = 10;
        double y = 3;

        System.out.println(x / y);

        x += 2;

        System.out.println(x);
        System.out.println("Result: " + x + " " + y);
    }
}
Show explanation

Output:

3.3333333333333335
12
Result: 12 3.0

x / y is double division because y is a double. Then x becomes 12. In the final line, the String comes first, so the remaining values are joined as text.

Primitive Types and Variables

These examples connect the common mistakes above to the core Unit 1 skills: using arithmetic, casting, and variables in realistic calculations.

Integer Division

int a = 17 / 5;
int b = 17 % 5;
System.out.println(a + " " + b);

Use division and modulus to break values into parts.

Casting

int correct = 17;
int total = 20;
double pct = (double) correct / total;
System.out.println(pct);

Cast before division when a decimal result is needed.

Physics Calculation

double v0 = 20.0;
double g = 9.8;
double t = 2.0;
double y = v0 * t - 0.5 * g * t * t;
System.out.println(y);

Translate physics formulas into Java syntax.

Try It in Java

Run and modify the examples below. Add print statements, change values, and test edge cases.

Code Tracing Drop-Down Practice

What prints? System.out.println(17 / 5);

It prints 3 because both operands are ints.

What prints? System.out.println("A" + 1 + 2);

It prints A12 because concatenation proceeds left to right.

What prints? System.out.println(1 + 2 + "A");

It prints 3A.

Debugging Practice

Why is double avg = correct / total; often wrong?

If both variables are ints, integer division happens first. Cast one operand to double.

Why is int x = 3.5; an error?

A double cannot be stored in an int without an explicit cast.

AP-Style Multiple Choice

Which expression gives the last digit of n?

n % 10

Which type stores true/false?

boolean

Which Math method gives a square root?

Math.sqrt(value)

FRQ Practice

Write a method public static double height(double v0, double t) that returns y = v0*t - 0.5*9.8*t*t. Then call it for t = 0, 1, 2, 3 and print the results.
Scoring focus

Look for the correct method signature, correct data use, clear control flow, and boundary-case handling.

Physics / Real-World Mini Project

Build a vertical motion calculator. Store initial velocity, time, and gravity as variables, calculate height, and print a clear sentence explaining the result.