Skip to content

Primitive data types

Java has eight primitive data types that represent simple values. They are not objects and are stored directly in memory.

TypeSizeRange / Values
byte1 byte-128 to 127
short2 bytes-32,768 to 32,767
int4 bytes-2³¹ to 2³¹‑1 (≈ ±2.1 billion)
long8 bytes-2⁶³ to 2⁶³‑1
float4 bytes±3.4E‑38 to ±3.4E+38 (6‑7 significant digits)
double8 bytes±1.7E‑308 to ±1.7E+308 (15 significant digits)
char2 bytes0 to 65,535 (Unicode characters)
booleannot precisely definedtrue or false
  • Performance: faster than objects because they live on the stack.
  • Simplicity: directly represent numbers, characters, and booleans.

Examples:

byte b = 100;
short s = 30000;
int i = 2_000_000; // underscores for readability
long l = 9_000_000_000L; // L suffix for long
float f = 3.14f; // f suffix for float
double d = 3.1415926535;
char c = 'A';
boolean flag = false;