01 / 22
int 0 1 0 1

Primitive Types
in Java

Master the four essential building blocks: int, double, boolean, and char to store and manipulate data at the most fundamental level.

AP Computer Science A

What Are Primitive Types?

In Java, every piece of data has a type that tells the compiler what kind of value it holds and how much memory to reserve. Primitive types are the simplest, most fundamental data types built directly into the language: they are not objects and have no methods.

Why Primitives Matter

Speed
Primitives are stored directly in memory, making operations on them extremely fast compared to objects.
Efficiency
They use a fixed, small amount of memory: an int always takes exactly 4 bytes.
Foundation
Every complex data structure in Java is ultimately built from primitives. They are the atoms of your programs.

The Four AP Primitives

int Whole numbers 4 bytes double Decimal numbers 8 bytes boolean true / false 1 bit (logical) char Single character 2 bytes 42 3.14 true 'A' STORED IN STACK MEMORY 00000000...00101010 0100...0001001... 0000000001000001

Declaring and Initializing

A variable declaration tells Java what type and name to use. Initialization gives it a value. You can do both in one line:

int age = 17; double gpa = 3.85; boolean isEnrolled = true; char grade = 'A';

The int Type

An int stores a 32-bit signed integer. Its range is from \( -2^{31} \) to \( 2^{31} - 1 \), which is approximately -2.1 billion to +2.1 billion.

int count = 0; int maxScore = 100; int negative = -42; int big = 2147483647; // max value

The double Type

A double stores a 64-bit floating-point number using the IEEE 754 standard. It can represent extremely large or small values with decimal precision, but it is not perfectly precise.

double pi = 3.14159265; double tiny = 0.001; double result = 7.0 / 2.0; // 3.5

boolean and char

boolean
Holds exactly two possible values: true or false. Used in conditions, loops, and logical expressions. Cannot be cast to or from numeric types.
char
Holds a single 16-bit Unicode character. Enclosed in single quotes: 'A', '7', '$'. Internally stored as an integer (its Unicode code point).
boolean isPassing = true; boolean done = false; char letter = 'B'; char digit = '9'; // character, not the number 9

Primitive vs. Reference Types

Primitive Reference (Object)
Stores The actual value A memory address
Default value 0, false, or '\u0000' null
Memory Stack (fixed size) Heap (variable size)
Has methods? No Yes
Examples int, double, boolean, char String, ArrayList, Scanner

Casting Between Types

char int double automatic automatic (char) cast (int) cast Widening (safe): no data loss Narrowing (unsafe): potential data loss
int x = 7; double y = x; // widening: y is 7.0 double pi = 3.14; int approx = (int) pi; // narrowing: approx is 3

Integer Arithmetic Rules

1
\( \texttt{int} + \texttt{int} \rightarrow \texttt{int} \): When both operands are ints, the result is always an int. No automatic promotion.
2
\( \texttt{int} / \texttt{int} \) truncates: Division discards the remainder. \( 13 / 5 \) evaluates to \( 2 \), not 2.6.
3
The \( % \) operator gives the remainder. \( 13 % 5 \) evaluates to \( 3 \).
4
\( \texttt{int} \text{ op } \texttt{double} \rightarrow \texttt{double} \): If either operand is a double, Java widens the int first and produces a double result.

Choosing the Right Type

1
Ask: is the value always a whole number? Counts, indices, ages, scores: use int.
2
Does it have decimals? Measurements, averages, money calculations: use double.
3
Is it a yes/no condition? Flags, toggles, test results: use boolean.
4
Is it a single character? Initials, menu choices, letter grades: use char.
5
When in doubt, check the operation. Division with ints truncates. If you need precision, ensure at least one operand is a double.

Example: Temperature Converter

Convert Fahrenheit to Celsius using the formula \( C = \frac{5}{9}(F - 32) \):

int fahrenheit = 98; double celsius = (5.0 / 9) * (fahrenheit - 32); // celsius is 36.666... // WRONG approach: double wrong = (5 / 9) * (fahrenheit - 32); // wrong is 0.0 because 5/9 = 0 (int division!)

Example: Grade Calculator

Calculate a student's average and determine if they pass:

int test1 = 85; int test2 = 92; int test3 = 78; double average = (test1 + test2 + test3) / 3.0; // average is 85.0 boolean passing = average >= 70.0; // passing is true char letterGrade = 'B';

Example: Clock Arithmetic

The modulus operator \( % \) is essential for cyclic patterns like clocks, days, and array wrapping:

int hour = 14; int display = hour % 12; // display is 2 (2:00 PM) int totalMinutes = 197; int hours = totalMinutes / 60; // 3 int mins = totalMinutes % 60; // 17 // 3 hours and 17 minutes boolean isEven = (42 % 2 == 0); // true

Common Mistakes

✕ Integer Division Surprise
Writing \( 1/3 \) expecting \( 0.333 \) but getting \( 0 \). Always make at least one operand a double: \( 1.0/3 \).
✕ Double Quotes for char
Using "A" instead of 'A'. Double quotes create a String, not a char. Java will refuse to compile.
✕ Using == for Doubles
Comparing doubles with \( == \) can fail due to floating-point imprecision. Check if the difference is smaller than a tiny threshold instead.
✕ Overflow Ignored
Adding \( 1 \) to Integer.MAX_VALUE wraps around to a large negative number without any error or warning.

Summary Table

Property int double boolean char
Size 32 bits 64 bits 1 bit (logical) 16 bits
Example 42 3.14 true 'Z'
Default 0 0.0 false '\u0000'
Division Truncates Keeps decimals N/A N/A
Literal form 42 3.14 true / false 'A'

Question 1 of 5

What is the value of \( \texttt{result} \) after this code runs?

int result = 17 / 5;

Question 2 of 5

Which declaration is incorrect Java syntax?

Question 3 of 5

What is the output of System.out.println(result)?

double result = 1 / 4 + 0.5;

Question 4 of 5

What does \( x % 10 \) evaluate to when \( x = 237 \)?

Question 5 of 5

What is the value of \( y \) after this code executes?

double x = 9.7;  int y = (int) x;