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.5Characters →
'A'Logical values →
true,falseObjects, 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:
| Category | Data Types |
| Integer | byte, short, int, long |
| Floating-point | float, double |
| Character | char |
| Boolean | boolean |
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:
trueorfalseDefault value:
falseJVM-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
Stringis 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
newOccupies 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)
| Feature | Primitive | Non-Primitive |
| Stores | Actual value | Memory address |
| Size | Fixed | Variable |
| Null allowed | ❌ No | ✅ Yes |
| Examples | int, char | String, Array |
5. Default Values (Very Important)
| Data Type | Default Value |
| byte, short, int, long | 0 |
| float | 0.0f |
| double | 0.0 |
| char | '\u0000' |
| boolean | false |
| Reference types | null |
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
intanddoubleare the most commonly usedJava is strongly typed
Choosing the right data type improves performance, safety and clarity