Skip to content

The eight primitive types and their literals

☕ Java 17 · Two Kinds of Data: Primitives and References · 1/3

Eight raw values

A primitive is a single value stored directly in the variable: no methods, never null. Java has exactly eight:

Type Holds Size Default (fields)
boolean true / false — false
byte whole number 8 bits 0
short whole number 16 bits 0
int whole number 32 bits 0
long whole number 64 bits 0L
float decimal number 32 bits 0.0f
double decimal number 64 bits 0.0
char one Unicode character 16 bits '\u0000'

Two families to remember:

  • Whole numbers: byte → short → int → long. Use int by default.
  • Decimals: float → double. Use double by default.

int goes up to about 2.1 billion. Beyond that, you need long.

Writing number literals

A literal is a value written directly in the code, like 14000 or 8.5. Java gives each literal a type:

int totalMovies = 14_000;             // whole number literal: int
long totalStreams = 3_500_000_000L;   // L suffix: long
double averageRating = 8.5;           // decimal literal: double
float storageGB = 4.2f;               // f suffix: float
Rule Example
A whole number literal is an int; add L for a long 3_500_000_000L
A decimal literal is a double; add f for a float 4.2f
Underscores are allowed between digits only 1_000_000 ✅ · _100, 100_, 10_.0 ❌

Prefer an uppercase L: a lowercase l looks like the digit 1.

These lines don't compile:

long bad1 = 3_500_000_000;   // ❌ too big for an int literal: add L
float bad2 = 4.2;            // ❌ 4.2 is a double: add f
double bad3 = _1000.0;       // ❌ an underscore can't start a number

Other number bases

Whole numbers can be written in other bases with a prefix:

int hexColor = 0xFF5733;   // hexadecimal: 0x, digits 0-9 and A-F
int binary   = 0b1101;     // binary: 0b, digits 0-1   (13)
int octal    = 017;        // octal: leading 0, digits 0-7   (15)

A leading 0 changes the meaning: 017 is fifteen, not seventeen.

Primitives cover single values. Everything else — text, movies, lists — is an object, reached through a reference.

💻 Code: Java17_Part_1_Fundamentals_Core_Syntax on GitHub

Next: reference types →