Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.89 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Primitive versus reference types
  3. The three primitive types
  4. Declaring and initialising variables
  5. Identifiers and constants
  6. Try this

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, true or false. Never put quotes around these; "true" is a String, not a boolean.

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 as 36.7 has a fractional part that int would truncate.

Q2. Identify why boolean ok = "true"; does not compile. [1 point]

  • Cue. "true" is a String (note the quotes), not the boolean literal true, 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

Sources & how we know this