Java Data Types & Type Casting Explained for Beginners
Introduction: Why Data Types Matter in Java
In Java, every variable must have a data type.
A data type tells Java:
What kind of data will be stored
How much memory is required
What operations can be performed
Understanding data types is essential because Java is a strongly typed language.
What Is a Data Type?
A data type defines the type and size of data a variable can store.
In simple words:
Data types tell Java what kind of value a variable holds.
Categories of Data Types in Java
Java data types are divided into two main categories:
Primitive Data Types
Non-Primitive (Reference) Data Types
Primitive Data Types in Java
Primitive data types store simple values directly in memory.
They are the most basic building blocks of Java programs.
List of Primitive Data Types
| Data Type | Size | Example | Description |
|---|---|---|---|
| byte | 1 byte | byte b = 10; | Very small integers |
| short | 2 bytes | short s = 200; | Small integers |
| int | 4 bytes | int a = 25; | Whole numbers |
| long | 8 bytes | long l = 1000L; | Large integers |
| float | 4 bytes | float f = 2.5f; | Decimal numbers |
| double | 8 bytes | double d = 3.14; | Large decimals |
| char | 2 bytes | char c = 'A'; | Single character |
| boolean | 1 bit | boolean flag = true; | True or false |
Example: Primitive Data Types
int age = 20;
double price = 99.99;
char grade = 'A';
boolean isPassed = true;
Non-Primitive Data Types in Java
Non-primitive data types store references (addresses) instead of actual values.
They are used to store complex data.
Common Non-Primitive Data Types
| Data Type | Description |
|---|---|
| String | Stores text |
| Array | Stores multiple values |
| Class | User-defined data type |
| Interface | Blueprint of a class |
Example: Non-Primitive Data Types
String name = "John";
int[] numbers = {1, 2, 3};
Key Differences: Primitive vs Non-Primitive
| Feature | Primitive | Non-Primitive |
|---|---|---|
| Stores | Actual value | Reference |
| Size | Fixed | Varies |
| Null value | Not allowed | Allowed |
| Methods | Not available | Available |
Real-Life Analogy for Data Types
Think of data types like containers:
Primitive → Small boxes for single items
Non-Primitive → Large boxes holding multiple items
You choose the container based on what you want to store.
What Is Type Casting in Java?
Sometimes, you may want to convert one data type into another.
This process is called type casting.
Type casting means changing the data type of a variable.
Types of Type Casting in Java
Java supports two types of type casting:
Widening Casting (Automatic)
Narrowing Casting (Manual)
Widening Type Casting (Automatic)
Definition
Converting a smaller data type into a larger data type automatically.
Example
int a = 10;
double b = a;
Here:
int→doubleNo data loss
Why It Is Safe
No loss of information
Done automatically by Java
Narrowing Type Casting (Manual)
Definition
Converting a larger data type into a smaller data type manually.
Example
double x = 9.8;
int y = (int) x;
Output:
9 Why Manual Casting Is Needed
Risk of data loss
Java requires explicit permission
Type Casting Chart
byte → short → int → long → float → double
- left to right → Widening
- Right to left → Narrowing
Frequently Asked Questions (FAQs)
Q1. Is type casting mandatory in Java?
Only required when converting larger data types to smaller ones.
Q2. Can boolean be type cast?
No. Boolean cannot be converted to other data types.
Q3. Is String a primitive data type?
No. String is a non-primitive (reference) data type.
Q4. Which casting is safer?
Widening casting is safer than narrowing casting.
Q5. Can data be lost during type casting?
Yes, during narrowing type casting.