Casting in Java
- Casting in Java is a way to convert a value from one data type to another.
- Think of it as transforming one type of object or value into another type.
- For example, converting a
double
number into anint
number.
Casting in Java are classified into two types:
- Primitive type casting
- Non-primitive type casting
1. Primitive Type Casting
This refers to casting primitive data types (like int
, char
, float
, etc.) to other primitive types. There are two types of primitive type casting:
- Implicit Casting (Widening): When a smaller primitive type is converted to a larger one. This is done automatically by Java without needing explicit instruction.
- Explicit Casting (Narrowing): When a larger primitive type is converted to a smaller one. This requires explicit casting because there’s a risk of losing data.
Examples of Primitive Type Casting:
Implicit Casting (Widening)
This happens when you assign a smaller data type to a larger data type. Java does it automatically because there’s no risk of data loss.
Example:
public class Main {
public static void main(String[] args) {
int num = 100; // int is 4 bytes
long longNum = num; // long is 8 bytes (implicit casting)
double doubleNum = longNum; // double is 8 bytes (implicit casting)
System.out.println(“Integer Value: ” + num); // 100
System.out.println(“Long Value: ” + longNum); // 100
System.out.println(“Double Value: ” + doubleNum); // 100.0
}
}
Explanation:
int
is automatically converted tolong
(widening).long
is automatically converted todouble
(widening).
Explicit Casting (Narrowing)
This happens when you try to convert a larger data type to a smaller one. You need to explicitly tell Java to do this conversion, and there’s a risk of losing data (e.g., decimal parts or overflow).
Example:
public class Main {
public static void main(String[] args) {
double num = 9.78; // double is 8 bytes
int intNum = (int) num; // explicit casting (narrowing from double to int)
System.out.println(“Double Value: ” + num); // 9.78
System.out.println(“Integer Value: ” + intNum); // 9 (decimal part lost)
}
}
Explanation:
- We explicitly cast the
double
(9.78) to anint
. This loses the decimal part (.78
), so the result is just9
.
2. Non-Primitive Type Casting (Object Casting)
Non-primitive type casting happens when you cast objects (instances of classes) from one type to another. This type of casting is especially used with inheritance, where one class is a subclass of another.
- Upcasting: Casting an object of a subclass type to a superclass type (automatic).
- Downcasting: Casting an object of a superclass type to a subclass type (requires explicit casting).
Examples of Non-Primitive Type Casting:
Upcasting (Implicit)
Upcasting is when you assign a subclass object to a superclass reference. This is automatic and safe, as every object of the subclass is also an object of the superclass.
Example:
class Animal {
void sound() {
System.out.println(“Some sound”);
}
}
class Dog extends Animal {
void sound() {
System.out.println(“Bark”);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog(); // Dog object
Animal animal = dog; // Upcasting (Dog to Animal)
animal.sound(); // Output: Bark
}
}
Explanation:
- We assign the
Dog
object to anAnimal
reference. Since aDog
is anAnimal
, the compiler allows this, and we can call thesound
method ofDog
.
- We assign the
Downcasting (Explicit)
Downcasting is when you assign a superclass reference to a subclass reference. This requires explicit casting, and it can result in a ClassCastException
if the object being cast is not of the subclass type.
Example:
class Animal {
void sound() {
System.out.println(“Some sound”);
}
}
class Dog extends Animal {
void sound() {
System.out.println(“Bark”);
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog(); // Upcasting (Dog to Animal)
Dog dog = (Dog) animal; // Downcasting (Animal to Dog)
dog.sound(); // Output: Bark
}
}
Explanation:
- We first upcast a
Dog
object to anAnimal
reference. - Then we downcast that
Animal
reference back to aDog
reference explicitly using(Dog)
.
Potential issue:
If we try to downcast an object that is not actually an instance of the subclass, it will throw a ClassCastException
.
Animal animal = new Animal(); // Animal object
Dog dog = (Dog) animal; // This will throw a ClassCastException at runtime
Explanation:
- The object
animal
is not an instance ofDog
, so trying to downcast it toDog
will cause an error.