Skip to main content

Command Palette

Search for a command to run...

Data Types in Java – Full Explanation

Published
5 min readView as Markdown
Data Types in Java – Full Explanation

Introduction

In Java, data types form the foundation of the language.
Before learning loops, classes, or frameworks, you must clearly understand how Java stores data in memory.

Java is a strongly typed language, which means:

  • Every variable must have a data type

  • Type correctness is checked at compile time

  • Errors are caught early, not at runtime

This design gives Java:

  • High safety

  • Strong reliability

  • Fewer unexpected crashes in production systems

That is why Java is widely used in banking, enterprise and large-scale applications.


1. What Is a Data Type?

A data type tells Java:

  • What kind of value a variable can store

  • How much memory should be allocated

  • What operations are allowed on that data

Examples:

  • Numbers → 10, 99.5

  • Characters → 'A'

  • Logical values → true, false

  • Objects, arrays, strings, etc.

Because Java is strongly typed:

A variable cannot store a value of a different type without explicit conversion.


2. Classification of Data Types in Java

Java data types are broadly divided into two main categories:

Data Types
│
├── Primitive Data Types
│
└── Non-Primitive (Reference) Data Types

PART 1: Primitive Data Types

Primitive data types are basic, built-in types that store actual values directly in memory.

✅ Total Primitive Data Types = 8

They are grouped into four categories:

CategoryData Types
Integerbyte, short, int, long
Floating-pointfloat, double
Characterchar
Booleanboolean

2.1 Integer Data Types

Used to store whole numbers (no decimal part).

1️⃣ byte

byte b = 10;
  • Size: 8 bits (1 byte)

  • Range: -128 to 127

  • Default value: 0

Use cases:

  • Memory-efficient large arrays

  • File handling

  • Network streams


2️⃣ short

short s = 32000;
  • Size: 16 bits (2 bytes)

  • Range: -32,768 to 32,767

  • Default value: 0

📌 Rarely used today
Mostly replaced by int


3️⃣ int ⭐ (Most Important)

int age = 25;
  • Size: 32 bits (4 bytes)

  • Range: −2³¹ to 2³¹−1

  • Default value: 0

📌 Most commonly used integer type in Java
Used for counters, loops, indexes and calculations.


4️⃣ long

long population = 1400000000L;
  • Size: 64 bits (8 bytes)

  • Range: Very large

  • Default value: 0

📌 Use cases:

  • Bank balances

  • Timestamps

  • Large numerical values

⚠️ L suffix is mandatory for literals.


2.2 Floating-Point Data Types

Used to store decimal values.

5️⃣ float

float pi = 3.14f;
  • Size: 32 bits

  • Precision: ~6–7 digits

  • Default value: 0.0f

⚠️ Requires f suffix
📌 Rarely used due to precision limitations


6️⃣ double ⭐ (Most Used)

double salary = 50000.75;
  • Size: 64 bits

  • Precision: ~15–16 digits

  • Default value: 0.0

📌 Default decimal type in Java
Used in most real-world calculations.


2.3 Character Data Type

7️⃣ char

char grade = 'A';
  • Size: 16 bits

  • Unicode-based

  • Range: 0 to 65,535

  • Default value: '\u0000'

📌 Can store:

  • Letters

  • Numbers

  • Symbols

  • Emojis 😄

char heart = '\u2665';

2.4 Boolean Data Type

8️⃣ boolean

boolean isJavaFun = true;
  • Values: true or false

  • Default value: false

  • JVM-dependent size

📌 Used in:

  • Conditions

  • Loops

  • Decision-making logic


PART 2: Non-Primitive (Reference) Data Types

Non-primitive data types do not store actual values.
They store references (memory addresses) pointing to objects in heap memory.


3. Types of Non-Primitive Data Types

1️⃣ String

String name = "Akash";
  • Stores sequence of characters

  • Immutable

  • Stored in String Constant Pool

  • String is a class, not a primitive


2️⃣ Arrays

int[] marks = {90, 85, 88};
  • Stores multiple values of same type

  • Fixed size

  • Index-based access


3️⃣ Classes

class Student {
    int id;
    String name;
}
  • Blueprint of objects

  • Combines data and methods


4️⃣ Objects

Student s1 = new Student();
  • Instance of a class

  • Created using new

  • Occupies memory in heap


5️⃣ Interfaces

interface Vehicle {
    void start();
}
  • Achieves abstraction

  • Supports multiple inheritance

  • Widely used in enterprise applications


4. Primitive vs Non-Primitive (Exam Favorite)

FeaturePrimitiveNon-Primitive
StoresActual valueMemory address
SizeFixedVariable
Null allowed❌ No✅ Yes
Examplesint, charString, Array

5. Default Values (Very Important)

Data TypeDefault Value
byte, short, int, long0
float0.0f
double0.0
char'\u0000'
booleanfalse
Reference typesnull

6. Why Java Has Primitive Types

Despite being object-oriented, Java includes primitives for:

✔ High performance
✔ Less memory usage
✔ Faster execution

Java carefully balances object-oriented design with system-level efficiency.


🎓 Professor’s Final Summary

  • Java has 8 primitive data types

  • Everything else is a reference type

  • int and double are the most commonly used

  • Java is strongly typed

  • Choosing the right data type improves performance, safety and clarity