A data type tells Java what kind of data a variable can store.
For example:
int age = 25;Here:
int→ data typeage→ variable25→ value
Java needs to know the data type because different types of data require different amounts of memory and support different operations.
For example:
int age = 25;
double price = 99.99;
char grade = 'A';
boolean isStudent = true;Here we are storing four different kinds of data.
1. Types of Data in Java
Java data types are broadly divided into two categories:
Data Types
│
┌──────────┴──────────┐
│ │
Primitive Non-Primitive
│ │
┌─────────┼─────────┐ ┌──────┼──────┐
│ │ │ │ │ │
Numeric Character Boolean String Array Class
│ (char) (boolean)
├──────────────────┐
│ │
Integer Decimal
(byte, short, (float, double)
int, long)There are 8 primitive data types in Java.
Non-primitive types include things like String, arrays, classes, objects, interfaces, and enums.
2. Primitive Data Types
Primitive data types are the basic data types built into Java.
Java has exactly 8 primitive types:
Type | Example | Purpose |
|---|---|---|
|
| Small integers |
|
| Small integers |
|
| Integer numbers |
|
| Large integers |
|
| Decimal numbers |
|
| Decimal numbers |
|
| Single character |
|
| True/false |
Let's understand them one by one.
byte
byte is used for small whole numbers.
byte age = 25;Range:
-128 to 127It takes 1 byte of memory.
short
short is used for whole numbers larger than byte.
short temperature = 300;Range:
-32,768 to 32,767It takes 2 bytes.
int
int is the most commonly used integer type.
int salary = 50000;
int age = 25;Range:
-2,147,483,648 to 2,147,483,647It takes 4 bytes.
For most normal integer values, use int.
long
long is used when the number is too large for an int.
long population = 8000000000L;Notice the L at the end. Without it, 8000000000 would be treated as an int literal, which is too large, and the code would not compile.
Range:
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807It takes 8 bytes.
Decimal Data Types
Java has two primitive types for decimal numbers:
float
doublefloat
float price = 99.99f;You generally need f at the end because Java treats decimal literals as double by default.
A float takes 4 bytes and is accurate to about 6-7 decimal digits.
double
double pi = 3.1415926535;double takes 8 bytes and is accurate to about 15-16 decimal digits, so it provides more precision than float.
Keep in mind that both float and double store approximate values. For example, 0.1 + 0.2 gives 0.30000000000000004. For exact money calculations, Java programs use the BigDecimal class.
For most decimal calculations, double is preferred.
char
char stores a single character.
char grade = 'A';
char symbol = '#';Notice that we use single quotes:
'A'Not:
"A""A" is a String, while 'A' is a char.
A char takes 2 bytes because Java uses Unicode (UTF-16). It stores values from 0 to 65,535, which is why char can also be treated as a number (for example, 'A' is 65).
boolean
boolean stores only two possible values:
true
falseExample:
boolean isLoggedIn = true;
boolean isJavaEasy = false;It is commonly used with conditions:
if (isLoggedIn) {
System.out.println("Welcome!");
}The exact memory size of a boolean is not fixed by the Java specification; it depends on the JVM. Also, unlike C/C++, Java does not treat 0 and 1 as false and true.
Summary of Sizes and Default Values
Type | Size | Default value (for fields) |
|---|---|---|
| 1 byte |
|
| 2 bytes |
|
| 4 bytes |
|
| 8 bytes |
|
| 4 bytes |
|
| 8 bytes |
|
| 2 bytes |
|
| JVM-dependent |
|
Default values apply to fields and array elements. Local variables have no default value and must be assigned before they are used.
3. Non-Primitive Data Types
Now let's look at non-primitive data types, also called reference types.
Examples include:
String
Array
Class
Object
Interface
EnumUnlike primitive types, these types are associated with objects or references to objects.
For example:
String name = "John";Here:
String → data type
name → variable
"John" → valueAnother example:
int[] numbers = {10, 20, 30};int[] is a non-primitive type because an array is an object.
Primitive vs Non-Primitive
The basic difference is:
Primitive
↓
Stores a value
Non-Primitive
↓
Stores a reference to an objectFor example:
int age = 26;Conceptually:
age
↓
26But:
String name = "John";Conceptually:
name
↓
reference
↓
"John" object4. Stack and Heap Memory

Now we can connect this to Stack and Heap memory.
When a Java program runs, the JVM uses different areas of memory.
Two important areas are:
Stack Memory
Heap MemoryA useful beginner mental model is:
Stack → method frames, local variables and references
Heap → objects and arrays
Primitive Example
Consider:
int age = 26;Conceptually:
STACK MEMORY
┌─────────────┐
│ age │
├─────────────┤
│ 26 │
└─────────────┘
HEAP MEMORY
┌─────────────┐
│ │
│ Nothing │
│ needed │
│ here for │
│ this local │
│ primitive │
└─────────────┘The important idea is:
The primitive variable contains the actual value.
age → 26Non-Primitive Example
Now consider:
String personName = "John";Conceptually:
STACK HEAP
┌───────────────┐ ┌───────────────┐
│ personName │ │ String Object │
├───────────────┤ ├───────────────┤
│ 0x0012 │────────────►│ "John" │
└───────────────┘ └───────────────┘
│
│
ReferenceThe variable personName contains a reference.
That reference points to the String object/data in the heap.
5. What About Arrays?
Arrays are also non-primitive/reference types.
Example:
int[] numbers = {10, 20, 30};Conceptually:
STACK HEAP
┌───────────────┐ ┌────────────────┐
│ numbers │ │ Array Object │
├───────────────┤ ├────────────────┤
│ 0x0020 │────────────►│ 10 │ 20 │ 30 │
└───────────────┘ └────────────────┘
│
ReferenceThe numbers variable contains a reference, while the array object is in the heap.
6. What About Objects?
Suppose we create a class:
class Student {
String name;
int age;
}Then:
Student student = new Student();Conceptually:
STACK HEAP
┌───────────────┐ ┌─────────────────┐
│ student │ │ Student Object │
├───────────────┤ ├─────────────────┤
│ 0x0030 │────────────►│ name │
└───────────────┘ │ age │
│ └─────────────────┘
ReferenceAgain:
student
↓
reference
↓
Student object7. The Big Picture
Now put everything together:
JAVA DATA TYPES
│
┌─────────────┴─────────────┐
│ │
PRIMITIVE NON-PRIMITIVE
│ │
Stores actual value Stores a reference
│ │
▼ ▼
STACK STACK → HEAP
│
▼
OBJECTFor example:
int age = 26;
String name = "John";Memory concept:
STACK HEAP
┌──────────────┐ ┌────────────────┐
│ age │ │ String Object │
│ 26 │ │ │
├──────────────┤ │ "John" │
│ name │────────────►│ │
│ 0x0012 │ └────────────────┘
└──────────────┘One detail: this picture is about local variables inside a method. If a primitive is a field of an object (like age inside a Student object), it is stored inside that object on the heap.
So the simple rule to remember is:
Primitive
Variable → Actual ValueNon-Primitive
Variable → Reference → Object
↓
Heap