Type Casting in Java
Type casting in Java refers to converting a variable of one data type into another. It is mainly classified into:
- Implicit Casting (Type Promotion) – Done automatically by Java when converting a smaller type to a larger type.
- Explicit Casting (Type Downgrade) – Done manually by the programmer when converting a larger type to a smaller type.
1. Implicit Casting (Type Promotion)
Happens automatically when converting a smaller type into a larger type. Since larger types can hold all possible values of smaller types, there’s no risk of data loss.
Example of Type Promotion
public class TypePromotion {
public static void main(String[] args) {
int num = 100;
double d = num; // int is promoted to double automatically
System.out.println(d); // Output: 100.0
}
}
Rules for Implicit Casting:
- byte → short → int → long → float → double
- char → int → long → float → double (because
char
is internally stored as an integer ASCII/Unicode value)
Example:
public class ImplicitCasting {
public static void main(String[] args) {
char ch = 'A';
int num = ch; // char is promoted to int (ASCII value of 'A' is 65)
System.out.println(num); // Output: 65
}
}
2. Explicit Casting (Type Downgrade)
When converting from a larger type to a smaller type, explicit casting is required. Since the smaller type may not be able to store all values of the larger type, data loss can occur.
Example of Type Downgrade
public class TypeDowngrade {
public static void main(String[] args) {
double d = 9.78;
int num = (int) d; // Explicitly casting double to int
System.out.println(num); // Output: 9 (fractional part is lost)
}
}
Rules for Explicit Casting:
- double → float → long → int → short → byte
- double → float → long → int → char
Example of Data Loss
public class ExplicitCasting {
public static void main(String[] args) {
int num = 257;
byte b = (byte) num; // Explicit cast from int to byte
System.out.println(b); // Output: 1 (because 257 % 256 = 1)
}
}
Explanation: Since byte
can only store values from -128 to 127, 257
exceeds this range, and only the remainder of (257 % 256 = 1)
is stored.
Key Differences Between Type Promotion and Type Downgrade
Feature | Type Promotion (Implicit) | Type Downgrade (Explicit) |
---|---|---|
Conversion | Smaller type → Larger type | Larger type → Smaller type |
Requires casting | No | Yes (explicit cast required) |
Risk of data loss | No | Yes (possible loss of precision or truncation) |
Example | int → double | double → int |
Type Casting with Objects
In Java, type casting also applies to objects when dealing with inheritance and interfaces. It includes:
- Upcasting (Implicit) – Converting a subclass reference to a superclass reference.
- Downcasting (Explicit) – Converting a superclass reference to a subclass reference (requires explicit casting).
Example of Upcasting (Implicit)
class Animal {
void makeSound() { System.out.println("Animal makes a sound"); }
}
class Dog extends Animal {
void bark() { System.out.println("Dog barks"); }
}
public class UpcastingExample {
public static void main(String[] args) {
Animal a = new Dog(); // Upcasting (implicit)
a.makeSound(); // Output: Animal makes a sound
}
}
Example of Downcasting (Explicit)
public class DowncastingExample {
public static void main(String[] args) {
Animal a = new Dog(); // Upcasting
Dog d = (Dog) a; // Downcasting (explicit)
d.bark(); // Output: Dog barks
}
}
💡 Caution: Downcasting without checking the actual type can cause ClassCastException
at runtime.
Conclusion
- Type Promotion (Implicit Casting): Java automatically converts a smaller type to a larger type.
- Type Downgrade (Explicit Casting): The programmer must manually cast when converting a larger type to a smaller type, with a risk of data loss.
- Objects also support casting, where upcasting is automatic, but downcasting requires an explicit cast.