W3docs

Java Data Types

Explore Java's primitive data types (byte, short, int, long, float, double, char, boolean) and reference types.

Java is statically typed — every variable has a type known at compile time. Types fall into two families: primitives (raw values built into the language) and reference types (objects, arrays, anything created with new). This chapter walks through both.

The eight primitive types

Primitives are the only values in Java that are not objects. They store their value directly in the variable's storage slot. Java defines exactly eight:

TypeSizeRangeDefaultExample literal
byte8 bits-128 to 1270byte b = 100;
short16 bits-32,768 to 32,7670short s = 30000;
int32 bits-2³¹ to 2³¹-1 (≈ ±2.1 billion)0int i = 1_000_000;
long64 bits-2⁶³ to 2⁶³-10Llong l = 9_000L;
float32 bitsIEEE 754 single precision0.0ffloat f = 3.14f;
double64 bitsIEEE 754 double precision0.0double d = 3.14;
char16 bitsUnicode code unit (U+0000 to U+FFFF)\0char c = 'A';
booleanJVM-definedtrue or falsefalseboolean b = true;

A few things worth committing to memory:

  • int is the default integer type. Most counters and indexes are int. Use long only when you actually need numbers above ~2 billion.
  • double is the default floating-point type. Plain 3.14 is a double; 3.14f is a float. Use double unless memory pressure forces float.
  • Underscores in numeric literals improve readability: 1_000_000 is the same as 1000000.
  • Default values apply only to fields, not to local variables. A local variable has no default — you must initialise it before reading.

Reference types

Everything else — String, arrays, your own classes, library classes — is a reference type. A reference-type variable doesn't hold the object itself; it holds a reference (essentially a pointer) to the object on the heap:

String name = "Ada";
int[] scores = new int[10];
LocalDate today = LocalDate.now();

The default value of any reference variable is null — a special value meaning "no object."

String s = null;
System.out.println(s.length());  // throws NullPointerException at runtime

null is the source of one of Java's most common runtime errors. Java NullPointerException covers patterns for avoiding it later.

Wrapper classes

Each primitive has a matching wrapper class — a real object that wraps the primitive value:

PrimitiveWrapper
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

You need wrappers when you want to put primitives into collections (which only hold objects):

List<Integer> scores = new ArrayList<>();
scores.add(42);   // autoboxed from int → Integer
int first = scores.get(0);  // unboxed from Integer → int

Java automatically converts between primitives and wrappers — called autoboxing and unboxing — so most of the time you don't notice.

A note on String

String is a reference type, not a primitive. It's special enough that the language gives it a literal syntax ("text") and the + operator. Strings are immutable — once created, their contents never change. Every "modification" returns a new string. The Java Strings chapter goes deep.

Choosing a type

A short decision tree for everyday Java:

  • Counting things or array indexesint
  • Large counts (file sizes, timestamps in millis)long
  • MoneyBigDecimal (not double — floating-point rounding will bite you)
  • Single characterchar
  • Yes/noboolean
  • TextString
  • A date or time → types in java.time (LocalDate, Instant, Duration)

Resist the urge to "save memory" by using byte or short for ordinary integers. JVMs are tuned for int operations; short doesn't actually use less memory in most cases.

A working demonstration

java— editable, runs on the server

What's next

Java Type Casting shows how to convert between numeric types and between related reference types.

Practice

Practice

Which statements about Java data types are correct?