What are the primitive types in Java, and how do you declare and initialise variables of each?
Topic 1.2 Variables and Data Types: identify the primitive types int, double and boolean, and declare, initialise and use variables of those types.
A focused answer to AP CSA Topic 1.2, covering the difference between primitive and reference types, the three exam primitives int, double and boolean, declaring and initialising variables, valid identifiers, and constants, with a traced worked example.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 1.2) wants you to distinguish primitive types from reference types, to know the three primitives the exam uses (int, double, boolean), and to declare and initialise variables of each correctly. Everything you compute in AP CSA is stored in a variable, so this topic underpins all arithmetic and logic.
Primitive versus reference types
The distinction matters because a primitive variable holds the value itself, while a reference variable holds a pointer to an object created elsewhere. Reference types are the subject of Unit 2; in Unit 1 you work with the three primitives.
The three primitive types
int- a 32-bit signed integer. Integer arithmetic truncates (no decimal part).double- a 64-bit floating-point number. Use it whenever a value can have a decimal part.boolean- holds one of two values,trueorfalse. Never put quotes around these;"true"is aString, not aboolean.
Declaring and initialising variables
A declaration introduces a variable and its type. Initialisation gives it a first value. You can do both at once:
int score; // declaration only
score = 10; // assignment
double rate = 0.05; // declaration and initialisation together
boolean active = true;
Reading a variable before it has been assigned a value is a compile-time error for local variables, so always initialise before you use.
When an int value is stored in a double variable, Java widens it automatically (double d = 5; stores 5.0). The reverse is not automatic: storing a double in an int would lose the fractional part, so Java requires you to cast explicitly (covered in Topic 1.5).
Identifiers and constants
An identifier (a variable or method name) must begin with a letter, underscore or dollar sign, may then contain letters and digits, cannot be a Java keyword (such as int, class, true), and is case sensitive - count and Count are different variables. The convention is camelCase for variables (finalScore, numStudents).
A variable declared final is a constant: it must be initialised and can never be reassigned. By convention constant names are written in all capitals.
final int MAX_SIZE = 100;
// MAX_SIZE = 200; // compile-time error: cannot reassign a final variable
Try this
Q1. State the most appropriate primitive type for storing a person's exact body temperature in degrees, and justify. [2 points]
- Cue.
double, because a temperature such as36.7has a fractional part thatintwould truncate.
Q2. Identify why boolean ok = "true"; does not compile. [1 point]
- Cue.
"true"is aString(note the quotes), not thebooleanliteraltrue, so the types do not match.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2021 (style)1 marksMultiple choice. Which of the following declarations and initialisations will compile without error?
(A) `int count = 3.5;`
(B) `double price = 5;`
(C) `boolean done = "false";`
(D) `int 2ndValue = 4;`
(E) `double rate = 2 / 0;`
Show worked answer →
The answer is (B).
(B) compiles: the int literal 5 is widened automatically to the double 5.0. (A) fails because assigning a double literal 3.5 to an int loses the fractional part and Java requires an explicit cast - it is a compile-time error. (C) fails because "false" is a String, not a boolean; the only boolean literals are true and false. (D) fails because an identifier cannot start with a digit. (E) compiles but is not a valid declaration-and-initialisation that runs cleanly: 2 / 0 is integer division by zero, a run-time error (and 2/0 is also int, not the point). The cleanest "compiles without error" answer is (B).
Markers reward knowing that an int widens to double automatically but a double does not narrow to int without a cast.
AP 2023 (style)3 marksFree response (code writing). Write a code segment that declares an `int` variable `students` initialised to 28, a `double` variable `average` initialised to 84.5, and a `boolean` variable `passed` initialised to `true`. Then print each on its own line in the form `students = 28`, `average = 84.5`, `passed = true`.
Show worked answer →
A 3-point question testing declaration, initialisation and output of all three primitives.
int students = 28;
double average = 84.5;
boolean passed = true;
System.out.println("students = " + students);
System.out.println("average = " + average);
System.out.println("passed = " + passed);
Point 1: correct types and initial values for all three variables. Point 2: string concatenation joining a label to each variable with +. Point 3: each printed on its own line with println and the exact label text. Declaring average as int would lose the .5; using "true" (a String) for passed would not compile.
Related dot points
- Topic 1.1 Why Programming? Why Java?: explain how a program is a sequence of statements executed in order, how Java source is compiled and run, and how to display output and handle simple runtime behavior.
A focused answer to AP CSA Topic 1.1, covering what a program is, the structure of a Java class with a main method, sequential execution, System.out output, and compile-time versus run-time errors, with a fully traced worked program.
- Topic 1.3 Expressions and Assignment Statements: evaluate arithmetic expressions using operator precedence, integer division and the modulo operator, and assign results to variables.
A focused answer to AP CSA Topic 1.3, covering arithmetic operators, operator precedence, integer division truncation, the modulo operator, and how assignment statements store results, with a fully traced worked evaluation.
- Topic 1.4 Compound Assignment Operators: use the compound assignment operators (+=, -=, *=, /=, %=) and the increment and decrement operators (++, --) as shorthand to update variables.
A focused answer to AP CSA Topic 1.4, covering the compound assignment operators, the increment and decrement operators, how each rewrites a longer assignment, and the integer-division traps they can hide, with a traced worked example.
- Topic 1.5 Casting and Ranges of Variables: use casting to convert between int and double, predict the effect of truncation, and recognize that an int has a finite range that can overflow.
A focused answer to AP CSA Topic 1.5, covering explicit casting between int and double, the truncation that casting to int causes, where the cast applies in an expression, the finite range of an int, and integer overflow, with a traced worked example.
- Topic 2.8 Wrapper Classes: Integer and Double: use the Integer and Double wrapper classes, including autoboxing and unboxing, the MIN_VALUE and MAX_VALUE constants, and parsing methods.
A focused answer to AP CSA Topic 2.8, covering why wrapper classes exist, creating Integer and Double objects, autoboxing and unboxing, Integer.MIN_VALUE and MAX_VALUE, parseInt and parseDouble, and the == versus equals trap for wrappers, with a worked trace.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)