Section 3.2 Typecasting
Typecasting is the process of converting a variable from one type to another. In Java, this is often necessary when you want to perform operations that require different data types. For example, if you have an integer and you want to convert it to a double for more precise calculations, you would use typecasting.
In Java, typecasting can be done in two ways: implicit and explicit. Implicit typecasting occurs automatically when converting from a smaller data type to a larger one (like int to double), while explicit typecasting requires you to specify the conversion manually (like double to int).
Implicit typecasting happens automatically when converting a value from a smaller data type to a larger one, as there is no risk of losing information. For example, you can assign an int to a double without any special syntax.
int myInt = 10; double myDouble = myInt; // Automatic casting from int to double
Explicit typecasting is required when converting from a larger data type to a smaller one, as you might lose data. You must do this manually by placing the target type in parentheses () before the value.
double originalDouble = 9.78; int castedInt = (int) originalDouble; // Explicitly casts double to int. The value of castedInt is now 9.
Besides primitive types, type casting is also a fundamental concept when working with objects, especially within an inheritance hierarchy. This involves converting an object reference from one class type to another, typically between a superclass and a subclass. This is often referred to as upcasting and downcasting.
class Animal {
public void makeSound() {
System.out.println("The animal makes a sound.");
}
}
class Dog extends Animal {
public void bark() {
System.out.println("The dog barks!");
}
}
Upcasting (Implicit): Upcasting is casting a subclass instance to a superclass reference type. This is always safe because a subclass object is guaranteed to have all the methods and properties of its superclass. Therefore, upcasting is done implicitly by the compiler.
// A Dog object is created, but the reference is of type Animal. // This is implicit upcasting. Animal myAnimal = new Dog(); myAnimal.makeSound(); // This is valid, as makeSound() is defined in Animal. // myAnimal.bark(); // This would cause a compile-time error! // The compiler only knows about the methods in the Animal reference type.
Downcasting (Explicit): Downcasting is casting a superclass reference back to its original subclass type. This is potentially unsafe because the superclass reference might not actually point to an object of the target subclass. You must perform an explicit cast. If you cast to the wrong type, Java will throw a
ClassCastException at runtime.
To safely downcast, you should first check the objectβs type using the
instanceof operator.
// 'myAnimal' is an Animal reference, but it points to a Dog object.
if (myAnimal instanceof Dog) {
// The check passed, so this downcast is safe.
Dog myDog = (Dog) myAnimal;
// Now we can access methods specific to the Dog class.
myDog.bark(); // This is now valid.
}
In this example, we first create a
Dog object and assign it to an Animal reference (upcasting). Then, we check if the Animal reference is actually pointing to a Dog object before downcasting it back to a Dog reference.
You have attempted of activities on this page.
