Type Casting
In Java, type casting is the process of converting a variable from one data type to another. This is necessary when you need to convert data from one type to another for various operations. Type casting is divided into two types: implicit casting (automatic) and explicit casting (manual).
1. Implicit Casting (Widening Conversion) #
Implicit casting is done automatically by the Java compiler when converting a smaller data type to a larger data type. This type of casting does not require explicit syntax and is safe because there is no risk of data loss.
Example:
public class Main {
public static void main(String[] args) {
int intValue = 100;
// Implicit casting from int to long
long longValue = intValue;
// Implicit casting from long to float
float floatValue = longValue;
System.out.println("Int value: " + intValue);
System.out.println("Long value: " + longValue);
System.out.println("Float value: " + floatValue);
}
}
Output:
Int value: 100
Long value: 100
Float value: 100.0
2. Explicit Casting (Narrowing Conversion)
Explicit casting is required when converting a larger data type to a smaller data type. This type of casting can result in data loss, so it must be done explicitly using the cast operator (type).
Example:
public class Main {
public static void main(String[] args) {
double doubleValue = 100.99;
// Explicit casting from double to int
int intValue = (int) doubleValue;
System.out.println("Double value: " + doubleValue);
System.out.println("Int value: " + intValue);
}
}
Output:
Double value: 100.99
Int value: 100
Casting Between Primitive Data Types
Widening (Implicit Casting)
- byte -> short -> int -> long -> float -> double
Example:
byte b = 10;
int i = b; // Implicit casting from byte to int
Narrowing (Explicit Casting)
- double -> float -> long -> int -> short -> byte
Example:
int i = 100;
byte b = (byte) i; // Explicit casting from int to byte
Casting Between Reference Types #
Casting between reference types involves converting objects of one type to another within the inheritance hierarchy.
Upcasting (Implicit)
- Converting a subclass type to a superclass type.
Example:
class Animal {}
class Dog extends Animal {}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
Animal animal = dog; // Implicit upcasting
}
}
Downcasting (Explicit)
- Converting a superclass type to a subclass type.
Example:
class Animal {}
class Dog extends Animal {}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
Dog dog = (Dog) animal; // Explicit downcasting
}
}
Summary #
- Implicit Casting (Widening): Automatic conversion from a smaller to a larger data type (e.g., int to long).
- Explicit Casting (Narrowing): Manual conversion from a larger to a smaller data type (e.g., double to int).
- Upcasting: Converting a subclass type to a superclass type.
- Downcasting: Converting a superclass type to a subclass type.
Understanding type casting is crucial for handling different data types and ensuring proper operations in your Java programs.