Java Basics: Variables & Data Types
AP Computer Science A — Java primitive types (int, double, boolean, char), the String object, declaration and casting. Worked example and a common exam trap.
AP Computer Science A (CSA) is taught in Java. Everything starts with declaring variables and choosing the right data type.
Primitive types
int— whole numbers (e.g. 42)double— decimals (e.g. 3.14)boolean—trueorfalsechar— a single character (e.g.'A')
String is not a primitive — it is an object that stores text: String name = "Uğur";
Declaring and assigning
int age = 17;
double gpa = 3.85;
boolean passed = true;
Integer division & casting
Dividing two int values truncates (drops the decimal):
int a = 7 / 2; // 3, not 3.5
double b = 7.0 / 2; // 3.5
double c = (double) 7 / 2; // 3.5 — cast first
Worked example
What is the value of 9 / 4 and 9 % 4 in Java?
9 / 4 is 2 (integer division); 9 % 4 is 1 (the remainder).
Exam tip
The most common multiple-choice trap is integer division: 5 / 2 is 2, not 2.5. To get a decimal result, at least one operand must be a double (or use a cast). Also remember char is compared/stored by its numeric code, and String values are compared with .equals(), not ==.
Short Lesson Video
Mock Exam
Practice Quiz
Test yourself: instant results and explanations.
1. In Java, what does 7 / 2 evaluate to?
2. Which primitive type stores true or false ?
3. What is the value of 9 % 4 in Java?
Need support with this topic?
In a free 30-minute intro call we assess your level and build a study plan tailored to you.
Free intro call