Chapter 07 of 77

Data Types in Java

A data type tells Java what kind of data a variable can store.

For example:

int age = 25;

Here:

  • int → data type

  • age → variable

  • 25 → 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

byte

byte x = 10;

Small integers

short

short x = 1000;

Small integers

int

int x = 100000;

Integer numbers

long

long x = 100000L;

Large integers

float

float x = 10.5f;

Decimal numbers

double

double x = 10.5;

Decimal numbers

char

char x = 'A';

Single character

boolean

boolean x = true;

True/false

Let's understand them one by one.


byte

byte is used for small whole numbers.

byte age = 25;

Range:

-128 to 127

It takes 1 byte of memory.


short

short is used for whole numbers larger than byte.

short temperature = 300;

Range:

-32,768 to 32,767

It 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,647

It 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,807

It takes 8 bytes.


Decimal Data Types

Java has two primitive types for decimal numbers:

float
double

float

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
false

Example:

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)

byte

1 byte

0

short

2 bytes

0

int

4 bytes

0

long

8 bytes

0L

float

4 bytes

0.0f

double

8 bytes

0.0

char

2 bytes

'\u0000'

boolean

JVM-dependent

false

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
Enum

Unlike primitive types, these types are associated with objects or references to objects.

For example:

String name = "John";

Here:

String → data type
name   → variable
"John"  → value

Another 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 object

For example:

int age = 26;

Conceptually:

age
 ↓
26

But:

String name = "John";

Conceptually:

name
 ↓
reference
 ↓
"John" object

4. Stack and Heap Memory

How Primitive and Non-Primitive Data Types Use Memory in Java

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 Memory

A 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 → 26

Non-Primitive Example

Now consider:

String personName = "John";

Conceptually:

             STACK                         HEAP

       ┌───────────────┐             ┌───────────────┐
       │ personName    │             │ String Object │
       ├───────────────┤             ├───────────────┤
       │    0x0012     │────────────►│    "John"     │
       └───────────────┘             └───────────────┘
             │
             │
        Reference

The 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   │
       └───────────────┘             └────────────────┘
             │
          Reference

The 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             │
             │                       └─────────────────┘
          Reference

Again:

student
   ↓
reference
   ↓
Student object

7. The Big Picture

Now put everything together:

                         JAVA DATA TYPES
                               │
                 ┌─────────────┴─────────────┐
                 │                           │
            PRIMITIVE                 NON-PRIMITIVE
                 │                           │
        Stores actual value          Stores a reference
                 │                           │
                 ▼                           ▼
              STACK                    STACK → HEAP
                                           │
                                           ▼
                                        OBJECT

For 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 Value

Non-Primitive

Variable → Reference → Object
                         ↓
                       Heap