Java Academy Logo

Data Types in Java

Data types in Java define the kind of data a variable can hold and the memory required to store it. They are broadly divided into two categories:

  • Primitive Data Types: Store simple values directly in memory.
  • Non-Primitive (Reference) Data Types: Store memory references to objects.

How Primitive Variables Are Stored

Primitive data types store the actual value directly in the stack memory.

Example:

int x = 10;
  • The variable x is stored in stack.
  • The actual value 10 is also in the stack.

✔ Primitive = store REAL value in stack

How Non-Primitive Variables Are Stored

Non-primitive data types store only the reference (address) in stack, but the actual object is stored in heap memory.

Example:

String name = new String("John");

This is what happens:

  • name → stored in stack
  • "John" object → stored in heap
  • Stack variable name stores a reference (pointer) to the heap object.

✔ Non-primitive = stack holds reference, heap holds actual object

Data Types Diagram

Primitive Data Types

Primitive types are the fundamental data types that store single values. Java defines eight primitive data types, summarized below:

TypeDescriptionDefaultSizeExampleRange
booleanLogical valuesfalseJVM-dependent (typically 1 byte)true, false
byte8-bit signed integer01 byte10-128 to 127
char16-bit Unicode character\u00002 bytes'A', '\u0041'0 to 65,535
short16-bit signed integer02 bytes2000-32,768 to 32,767
int32-bit signed integer04 bytes1000, -500-2,147,483,648 to 2,147,483,647
long64-bit signed integer0L8 bytes123456789L±9.22e18
float32-bit floating point0.0f4 bytes3.14f~6–7 digits precision
double64-bit floating point0.0d8 bytes3.14159d~15–16 digits precision