Advertisement
Beginner Java, Java Fundamentals: Syntax, Data Types & Operators

What are the eight primitive data types in Java, and how do they differ fundamentally from reference types like String or a custom class?

Short answer

The eight primitives are byte, short, int, long (integer types of increasing size), float, double (floating-point types), char (a single 16-bit Unicode character), and boolean (true/false) -- primitives store their actual value directly in the variable's own memory location…

Save Java mock interview
Advertisement
AModel answer
The eight primitives are byte, short, int, long (integer types of increasing size), float, double (floating-point types), char (a single 16-bit Unicode character), and boolean (true/false) -- primitives store their actual value directly in the variable's own memory location (whether a stack frame slot or as part of an object's fields), have a fixed, well-defined size and value range, and always have a sensible zero-equivalent default, while reference types store a reference (pointer) to an object's data located elsewhere on the heap, default to null, and can be arbitrarily complex, custom-defined, or even represent primitive-like values via boxing (Integer wrapping an int).
int primitiveInt = 42;           // stores the value 42 directly
Integer boxedInt = 42;            // stores a REFERENCE to a heap-allocated Integer object holding 42

boolean isActive = true;          // direct value
String name = "Alice";            // reference to a heap-allocated String object
String nullName = null;           // reference types can be null; primitives never can
Real-world example
A performance-sensitive numerical calculation loop deliberately uses primitive int and double variables rather than their boxed Integer/Double wrapper equivalents, avoiding both the memory overhead of heap-allocated wrapper objects and the CPU cost of repeated auto-boxing/unboxing across millions of loop iterations.
Likely follow-ups
  • Why can't a primitive type ever be null, unlike every reference type?
  • What's the memory size in bytes of each of the eight primitive types?
Related topics