Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 02 / Topic 2 of 9

The eight primitive types

Java has exactly eight primitive types: byte, short, int, long (whole numbers), float, double (decimals), char (one character) and boolean (true/false). String is not in this list — it is a class, used constantly but not covered until Stage 2.

Type Size Range (whole numbers) Typical use
byte 8 bits -128 to 127 rarely used directly; raw bytes
short 16 bits -32,768 to 32,767 rarely used directly
int 32 bits -2,147,483,648 to 2,147,483,647 the default whole-number type
long 64 bits about ±9.2 × 10¹⁸ big counters, IDs; literal needs L
float 32 bits ~7 correct decimal digits rarely used; literal needs f
double 64 bits ~15 correct decimal digits the default decimal type
char 16 bits one UTF-16 character single characters
boolean 1 bit (conceptually) true / false conditions, flags
long population = 1_428_600_000L;   // bigger range, needs the L suffix
float weightKg = 2.5f;              // needs the f suffix
char currencySymbol = '₹';

Output (examples/02_primitive_types.java):

120
30000
100234
1428600000
2.5
2450.75
₹
false
-128
0.33333334
0.3333333333333333
1000000

Two things to notice in that output:

  • Overflow is silent. byte maxByte = 127; byte overflowed = (byte) (maxByte + 1); gives -128, not an error. A byte (or int, long, …) just wraps around past its range.
  • float loses precision that double keeps. 1.0f / 3.0f prints 0.33333334 (8 digits); 1.0 / 3.0 prints 0.3333333333333333 (16 digits). Use double unless you have a specific reason not to (large arrays of numbers, interop with older APIs).

Underscores in a number literal (1_000_000) are only for human readers; the compiler ignores them.

🔍 Going deeper: int is the default for whole-number literals and double for decimal literals — write 3 and Java treats it as int, write 3.0 and it is double. This is why 100 / 3 is 33, not 33.33: both sides are already int before any variable gets involved.

Full program: examples/02_primitive_types.java.

Done here? The practice set for Part 2 — including the solutions — is on the part page.

Search the guide