skip to content

What are the eight primitive types in Java, and what is the size of each?

level: juniorimportance: must knowfreq 85%

answer

  1. 8 types: byte short int long, float double, char boolean
  2. int=32, long=64, byte=8, short=16 (doubling)
  3. float=32 double=64 (IEEE 754)
  4. char=16-bit UNSIGNED, boolean size unspecified

basics

~10 s

Java has eight primitives: byte (8 bits), short (16), int (32), long (64), float (32), double (64), char (16), and boolean. They hold simple values directly, not objects.

solid answer

~40 s

Java defines exactly eight primitive types. Four are integer types: byte (8-bit), short (16-bit), int (32-bit), and long (64-bit), all signed two's-complement. Two are floating-point: float (32-bit IEEE 754 single precision) and double (64-bit double precision). char is a 16-bit unsigned value holding a UTF-16 code unit (0 to 65535). boolean holds true or false; its size is not specified by the JVM. Primitives store their value directly rather than a reference to a heap object, so they have no methods and cannot be null. Sizes are fixed by the language spec and identical on every platform, unlike C where int width can vary.

go deeper

for a junior

Can list all eight primitives and give the bit size of the common ones (int 32, long 64, double 64, boolean).

for a middle

Knows every size precisely including byte/short/char, knows char is 16-bit unsigned and boolean size is unspecified, and can state ranges.

for a senior

Explains two's-complement and IEEE 754 representation, justifies fixed sizes for portability, and reasons about memory/overflow trade-offs when choosing a type.

for a principal

Frames primitive choice in terms of cache footprint, JIT/boxing costs, value-type evolution (Project Valhalla), and sets team conventions for numeric type selection.

## What is a primitive type? A *type* tells the compiler what kind of value a variable holds and what operations are legal on it. Java splits all types into two families: **primitive types** and **reference types**. A reference type (like `String` or any class) stores a *reference* — essentially a pointer to an object living on the heap. A **primitive type** stores the actual value directly in the variable's memory slot. Primitives have no methods, cannot be `null`, and are not objects. Java defines **exactly eight** primitive types, and the *language specification fixes their sizes* — they are identical on every operating system and CPU. (In C/C++ the width of `int` can vary by platform; Java deliberately removed that ambiguity.) ## The eight types **Integer types** (whole numbers, stored in *two's-complement*, a binary scheme where the top bit is the sign and negatives are represented by inverting bits and adding one): - **byte** — 8 bits. Range -128 to 127. - **short** — 16 bits. Range -32,768 to 32,767. - **int** — 32 bits. Range about -2.1 billion to +2.1 billion (-2^31 to 2^31-1). This is the default integer type. - **long** — 64 bits. Range about -9.2 quintillion to +9.2 quintillion (-2^63 to 2^63-1). Written with an `L` suffix, e.g. `10_000_000_000L`. **Floating-point types** (numbers with a fractional part, stored using the *IEEE 754* standard, which splits the bits into a sign, an exponent, and a fraction/mantissa): - **float** — 32 bits, single precision, about 7 decimal digits of accuracy. Written with an `f` suffix, e.g. `3.14f`. - **double** — 64 bits, double precision, about 15-16 decimal digits. This is the default for decimal literals. **Character type:** - **char** — 16 bits, **unsigned**, range 0 to 65,535. It holds one UTF-16 *code unit* (Java's representation of a Unicode character). Because it is unsigned and numeric, a `char` can be used in arithmetic, e.g. `'A' + 1` equals 66. **Boolean type:** - **boolean** — holds only `true` or `false`. The JVM specification does **not** define its size; in practice it often occupies a full byte (or an int slot on the operand stack), but you must not rely on any specific size. ## Why this matters Knowing the sizes lets you reason about *range* (will my value overflow?), *memory* (a `long[]` of a million elements is 8 MB), and *correctness* (using `int` for money in cents up to ~21 million is fine; for more you need `long` or `BigDecimal`). Every level of engineer is expected to recall these eight types and their bit widths instantly.

  • Why does Java fix primitive sizes when C does not?
    Java targets a portable virtual machine: fixing sizes guarantees a program behaves identically on every platform (write once, run anywhere), removing the platform-dependent integer-width bugs common in C.
  • Which integer type is the default, and what suffix does long need?
    int is the default integer type; a long literal that exceeds int range needs an L suffix, e.g. 5_000_000_000L.

saying these in an interview costs you the question

  • Claiming String is a primitive (it is a class / reference type)
  • Saying char is signed (it is unsigned, 0-65535)
  • Giving a fixed size for boolean (the JVM does not specify it)
  • Confusing float (32) and double (64), or thinking double is the default-suffix type needing 'd'

context