When working with data in Java, you often need to convert one data type into another. This is where type casting and type conversion come into play.
👉 These concepts are critical for:
- Data manipulation
- Calculations
- Avoiding errors
- Writing efficient programs
🔹 What is Type Conversion?
Type Conversion refers to automatically converting one data type into another by Java.
👉 It happens implicitly (automatically) when:
- No data loss occurs
- Converting from smaller → larger type
🔸 Example (Implicit Conversion / Widening)
int a = 10;
double b = a; // int → double
System.out.println(b); // 10.0
👉 Here:
-
Java automatically converts
inttodouble - No data loss
🔹 What is Type Casting?
Type Casting is when you manually convert one data type into another.
👉 Required when:
- Converting larger → smaller type
- Data loss may occur
🔸 Example (Explicit Casting / Narrowing)
double x = 10.75;
int y = (int) x;
System.out.println(y); // 10
👉 Here:
- Decimal part is removed
- Data loss happens
🔹 Types of Type Casting in Java
Java supports two types:
- Widening Casting (Implicit)
- Narrowing Casting (Explicit)
🔹 1. Widening Casting (Automatic)
Converting smaller data type → larger data type.
🔸 Conversion Order:
byte → short → int → long → float → double
🔸 Example:
int num = 50;
double result = num;
System.out.println(result); // 50.0
👉 Safe conversion
👉 No manual effort needed
🔹 2. Narrowing Casting (Manual)
Converting larger data type → smaller data type.
🔸 Example:
double value = 99.99;
int result = (int) value;
System.out.println(result); // 99
👉 Requires explicit casting
👉 Data loss possible
🔹 Why Type Casting is Needed
In real-world applications, you often:
- Read data from users
- Perform calculations
- Convert between types
👉 Example:
int a = 10;
int b = 3;
double result = (double) a / b;
System.out.println(result); // 3.333...
👉 Without casting:
3
👉 With casting:
3.333...
🔹 Type Casting with Characters
Java internally treats characters as numbers (ASCII/Unicode).
🔸 Example:
char ch = 'A';
int value = ch;
System.out.println(value); // 65
🔸 Reverse:
int num = 66;
char ch = (char) num;
System.out.println(ch); // B
🔹 Type Casting in Expressions
Java automatically promotes smaller types during expressions.
🔸 Example:
byte a = 10;
byte b = 20;
// int result = a + b; // auto promotion to int
int result = a + b;
System.out.println(result); // 30
👉 Even though both are byte, result becomes int.
🔹 Real-Life Example
int totalMarks = 450;
int subjects = 5;
double average = (double) totalMarks / subjects;
System.out.println("Average: " + average);
👉 Used in:
- Student results
- Calculations
- Financial systems
🔹 Common Mistakes
❌ Forgetting type casting in division
❌ Assuming automatic conversion always happens
❌ Ignoring data loss
❌ Misusing casting in large values
🔹 Important Rules
✔ Widening → Automatic
✔ Narrowing → Manual
✔ Casting syntax → (type)value
✔ Data loss occurs in narrowing
🔹 Best Practices
✔ Use widening whenever possible
✔ Avoid unnecessary casting
✔ Be careful with precision loss
✔ Use casting in calculations when needed
🔹 Interview Questions
- What is type casting in Java?
- Difference between widening and narrowing
- Why casting is needed in division?
- What happens during data loss?
🔹 What to Learn Next
After this topic, move to:
- Control Statements (if, switch)
- Loops (for, while)
- Arrays & Strings
👉 This will help you build logic.
🔹 Conclusion
Type casting and type conversion are essential for:
- Data transformation
- Accurate calculations
- Writing flexible programs
👉 Mastering this helps you avoid bugs and write better code.
Remember:
Right data type + correct conversion = correct output
Comments
Post a Comment