Java Data Types
✅ Java Data Types
In Java, every variable must have a data type. Data types define the size and type of value that variables can store.
Java data types are divided into two main categories:
🧩 1️⃣ Primitive Data Types
Primitive types are the most basic data types and store simple values. There are 8 primitive types in Java:
| Type | Size | Example Value | Description |
|---|---|---|---|
byte |
1 byte | -128 to 127 |
Small integer |
short |
2 bytes | -32,768 to 32,767 |
Small integer |
int |
4 bytes | -2B to 2B |
Standard integer |
long |
8 bytes | Very large values | Integer with ‘L’ |
float |
4 bytes | Decimal (6–7 digits) | Use f |
double |
8 bytes | Decimal (15 digits) | More precise |
boolean |
1 bit | true or false |
Logical values |
char |
2 bytes | 'A', '9', '$' |
Single character |
📌 Example: Primitive Data Types in Java
🧩 2️⃣ Non-Primitive (Reference) Data Types
These types store memory references, not direct values.
Examples include:
| Type | Example |
|---|---|
String |
"Hello World" |
Arrays |
{1, 2, 3} |
Classes |
Student obj = new Student(); |
Objects |
Any custom type |
Interfaces |
Advanced Java concept |
📌 Example: Non-Primitive Data Types
📍 Key Differences: Primitive vs Non-Primitive
| Feature | Primitive | Non-Primitive |
|---|---|---|
| Size | Fixed | Not fixed |
| Stored in | Stack memory | Heap memory |
| Methods | No built-in methods | Have built-in methods |
| Examples | int, float, boolean |
String, Array, Class |
🔍 Type Suffix Rules
| Type | Suffix Example |
|---|---|
long |
100L |
float |
10.5f |
Without suffix, Java treats decimals as double.
✔ Mini Practical Example
🎯 Summary
-
Java has 8 primitive types
-
Non-primitive types include
String, arrays, and objects -
Data types determine memory size and operations allowed
