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 ==.
Courte vidéo de cours
Examen blanc
Quiz d'entraînement
Teste-toi : résultats et explications immédiats.
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?
Besoin d'aide sur ce sujet ?
Lors d'un appel de découverte gratuit de 30 minutes, nous évaluons ton niveau et construisons un plan d'étude sur mesure.
Appel de découverte gratuit