Java Academy Logo

Data Types in Java

Java data types define the kind of data a variable can hold and how memory is allocated. Learn about primitive types, reference types, and memory storage.

Overview of Data Types

Data types in Java are broadly categorized into:

  • 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 types store the actual value directly in the stack memory.

Example:

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

✔ Primitive = store REAL value in stack

How Non-Primitive Variables Are Stored

Non-primitive types store a reference in the stack, but the actual object is stored in the heap memory.

Example:

String name = new String("John");

Memory behavior:

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

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

Diagram showing Java primitive and non-primitive data types

Primitive Data Types

Java has eight primitive data types. They store single values directly and are stored in stack memory.

TypeDescriptionDefaultSizeExampleRange
booleanLogical valuesfalse1 byte (JVM dependent)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