Introduction
In Java programming, data types play a crucial role. Sometimes, we need to convert one data type into another β either to perform calculations, store values, or pass data to methods.
This conversion process is called Type Casting π―
Understanding type casting properly is very important because:
- It prevents data loss
- It avoids compile-time errors
- It helps in working with different data types
- It is heavily used in real-world applications
Letβs understand everything step-by-step.
π§ What is Type Casting?
Type Casting is the process of converting one data type into another.
In Java, type casting is divided into:
1οΈβ£ Primitive Type Casting
2οΈβ£ Object Type Casting
π 1. Primitive Type Casting
Primitive casting happens between basic data types like:
- byte
- short
- int
- long
- float
- double
- char
Primitive casting is of two types:
- Widening (Implicit Casting)
- Narrowing (Explicit Casting)
π’ Widening Casting (Implicit Casting)
Also called:
β Automatic casting
β Upcasting (for primitives)
Happens when:
- Smaller data type β Larger data type
- No data loss
Order of widening:
byte β short β int β long β float β double
π’ Example 1: Widening Casting
public class WideningDemo {
public static void main(String[] args) { int num = 100;
double d = num; // Automatic casting System.out.println("Integer value: " + num);
System.out.println("Converted to double: " + d);
}
}
π§ Explanation
numis int- int is converted automatically to double
- No cast operator needed
- No data loss
π΄ Narrowing Casting (Explicit Casting)
Also called:
β Manual casting
β Downcasting (for primitives)
Happens when:
- Larger data type β Smaller data type
- Possible data loss
You must use cast operator.
π’ Example 2: Narrowing Casting
public class NarrowingDemo {
public static void main(String[] args) { double d = 10.75;
int num = (int) d; // Explicit casting System.out.println("Double value: " + d);
System.out.println("Converted to int: " + num);
}
}
π§ Explanation
- Double contains decimal
- Casting to int removes decimal part
- Output: 10
- Data loss occurs
β Important Data Loss Example
public class DataLossDemo {
public static void main(String[] args) { int num = 130;
byte b = (byte) num; System.out.println(b);
}
}
π§ Why Output Is Strange?
- byte range: -128 to 127
- 130 exceeds byte range
- Overflow happens
This causes unexpected output.
π― Casting Between int and char
public class CharCasting {
public static void main(String[] args) { int num = 65;
char ch = (char) num; System.out.println(ch);
}
}
Output:
A
Because ASCII value of 65 is βAβ.
π 2. Object Type Casting
Object casting happens in OOP when:
- One class reference is converted into another class reference
Object casting is of two types:
1οΈβ£ Upcasting
2οΈβ£ Downcasting
π’ Upcasting
Converting child class object to parent class reference.
Automatically done by Java.
π’ Example 3: Upcasting
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}public class UpcastingDemo {
public static void main(String[] args) { Animal a = new Dog(); // Upcasting a.sound();
}
}
π§ Explanation
- Dog object stored in Animal reference
- Only parent methods accessible
- Safe casting
π΄ Downcasting
Converting parent reference to child reference.
Requires explicit cast.
π’ Example 4: Downcasting
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}public class DowncastingDemo {
public static void main(String[] args) { Animal a = new Dog();
Dog d = (Dog) a; // Downcasting d.bark();
}
}
π§ Explanation
- Parent reference converted back to child
- Explicit cast required
- Safe because object is originally Dog
β Dangerous Downcasting Example
Animal a = new Animal();
Dog d = (Dog) a; // Runtime error
This causes:
ClassCastException
π§ How To Avoid Runtime Error?
Use:
if(a instanceof Dog) {
Dog d = (Dog) a;
}
π Primitive vs Object Casting
| Primitive Casting | Object Casting |
|---|---|
| Between data types | Between class references |
| int β double | Child β Parent |
| Uses type operator | Uses type operator |
| Compile-time check | Runtime check |
π₯ Common Mistakes
β Forgetting cast operator
double d = 10.5;
int x = d; // Error
β Ignoring data loss
int x = (int) 9.9; // 9, not 10
β Unsafe downcasting
Parent p = new Parent();
Child c = (Child) p; // Runtime error
π§ Interview Questions
β Difference between widening and narrowing
β What happens during overflow?
β What is upcasting and downcasting?
β What is ClassCastException?
β Why explicit casting required in narrowing?
π Real-World Use Cases
- Parsing data
- Numeric calculations
- Working with generics
- Polymorphism
- Memory optimization
π― Quick Summary
β Widening β Automatic, Safe
β Narrowing β Manual, Risky
β Upcasting β Automatic
β Downcasting β Manual, Risky
β Always check before downcasting
β Conclusion
Type Casting is a fundamental concept in Java that ensures flexibility when working with different data types and objects π
Understanding:
- Primitive casting
- Widening & narrowing
- Upcasting & downcasting
- Data loss
- Runtime errors
Helps you write safer and more efficient programs.
Mastering type casting strengthens your understanding of Javaβs type system and object-oriented design.