The type casting mechanism in Java allows programmers to explicitly or implicitly convert a value from one type to another. Historically, this feature is inherited from C and C++, but in Java, it is limited to increase type safety and prevent hidden bugs related to overflow or data loss.
The problem lies in the possibility of ClassCastException occurring during the casting of reference types, as well as the loss of precision when casting primitive types, for example, when converting from double to int. Logical errors can occur during so-called "downcasting" (casting to a subtype) if the instance does not belong to that class.
The solution is a strict separation:
Example code for primitives:
int i = 100; long l = i; // implicit casting (int -> long) double d = l; // implicit (long -> double) int i2 = (int) d; // explicit casting (fractional part lost)
Example code for reference types:
Object obj = "Hello"; // upcasting, implicitly String s = (String) obj; // downcasting, explicitly
Key features:
Can the Java compiler prevent any erroneous type casting?
Answer: No, the compiler catches only obvious errors during compilation. If casting is possible in the type structure (e.g., Object -> String), but the variable actually holds an object of an incompatible type, the error will only manifest at runtime with ClassCastException.
Does Integer inherit from Long, and can I write Integer i = (Integer) someLong?
Answer: No, Integer and Long are independent wrapper classes; downcasting between them is not possible. They both inherit from Number, but not from each other. Casting like (Integer) (Object) 1L; will cause ClassCastException.
Is rounding applied when explicitly casting float to int?
Answer: No, the fractional part is truncated without rounding:
float f = 3.99f; int i = (int) f; // i == 3, not 4
A developer receives a collection of Object, casts each element to its type but does not check instanceof. The project consistently crashes with ClassCastException on incorrect data.
Pros:
Cons:
All downcasting operations are performed inside if (obj instanceof TargetType) with explicit error handling. Generics are applied for collections.
Pros:
Cons: