AP CSA • CS II/III Fundamentals Java

Java Primitive Data Types

Concise notes, examples, and a reference table for all 8 primitives.

1) Introduction

Java has 8 primitive data types. These are the most basic containers for values and are not objects. All other data structures (like String, arrays, and classes) build on top of these primitives.

Why it matters: choosing the right primitive affects memory, speed, and precision.

2) The 8 Primitive Data Types

A) Integer Types

  • byte — 8 bits; range: −128 .. 127
  • short — 16 bits; range: −32,768 .. 32,767
  • int — 32 bits; range: −2,147,483,648 .. 2,147,483,647 (default for whole numbers)
  • long — 64 bits; very large range (use L suffix)

byte

byte age = 25;

Great for memory‑tight arrays or raw data (files, streams).

short

short year = 2025;

Use rarely; typically int is fine unless memory is critical.

int

int population = 330_000_000;

Default integer type. Readable underscores are allowed in numeric literals.

long

long distanceToMars = 225_000_000_000L;

Remember the L suffix to avoid int overflow.

B) Floating‑Point Types

  • float — 32 bits; ~7 decimal digits; needs an f suffix
  • double — 64 bits; ~15 decimal digits; default for decimals

float

float pi = 3.1415927f;

Use when memory is tight or for large float arrays (graphics, sensors).

double

double gravity = 9.80665;

Default for scientific/financial approximations (but see precision note below).

C) Other Types

  • char — 16‑bit Unicode character (single quotes)
  • boolean — logical true/false

char

char grade = 'A';
char symbol = '#';
char newline = '\n';

Stores a single UTF‑16 code unit; escape sequences like \n are allowed.

boolean

boolean isReady = true;
boolean doorOpen = false;

Drives control flow in if, while, and logical operations.

3) Default Values (for fields, not locals)

TypeDefault
byte, short, int, long0
float, double0.0
char'\u0000' (null char)
booleanfalse

Important: Local variables have no default—Java requires you to initialize them before use.

4) Numeric Literals & Suffixes

  • Underscores improve readability: int n = 1_000_000;
  • Binary/hex supported: int mask = 0b1010;, int color = 0xFFEECC;
  • long needs L (or l): long big = 3_000_000_000L;
  • float needs f: float r = 2.5f;

5) Casting & Type Promotion

Widening (implicit)

int x = 10;
double y = x; // OK: int → double

Safe: smaller → larger container.

Narrowing (explicit)

double a = 9.8;
int b = (int) a; // 9 (truncates)

Risk of data loss: fractions chopped, ranges overflow.

In arithmetic, byte and short are promoted to int before operations. Mixing int and double yields double.

6) Overflow, Precision, and BigDecimal

  • Overflow: integers wrap around silently.
    int max = Integer.MAX_VALUE;
    int oops = max + 1; // wraps negative
  • Floating point: float/double are approximate (binary IEEE‑754).
    double sum = 0.1 + 0.2; // 0.30000000000000004
  • For money, prefer java.math.BigDecimal.
    import java.math.BigDecimal;
    BigDecimal price = new BigDecimal("19.99");

7) Quick Reference Table

Type Bits Typical Range / Precision Example
byte8−128 .. 127byte b = 10;
short16−32,768 .. 32,767short s = 300;
int32~ −2.1B .. +2.1Bint n = 5_000;
long64Very largelong id = 9_000_000_000L;
float32~7 decimal digitsfloat f = 3.14f;
double64~15 decimal digitsdouble d = 3.14159;
char16UTF‑16 code unitchar c = 'A';
booleantrue / falseboolean ok = true;

8) Ready‑to‑Use Snippets

Demo: declarations & casting

// Declarations
int count = 42;
long big = 3_000_000_000L;
double price = 19.99;
float ratio = 0.75f;
char initial = 'J';
boolean isAdmin = false;

// Widening: int → double
double cost = count;

// Narrowing: double → int (truncates)
int whole = (int) price;

// Promotion in expressions (byte/short → int, int + double → double)
short a = 5;
short b = 7;
int sum = a + b;
double avg = sum / 2.0;

Practice prompts

  1. Pick the smallest integer type that can store the value 50,000. Explain why.
  2. Why is double preferred over float for most decimal work?
  3. Show an example where integer overflow occurs. How could you prevent it?