Hello world,
This is my first post so please bear with me.
I'm studying for
SCJP 6 using Kathy Sierra and Bert Bates' study guide.
My question is what is the impact of the instanceof operator in snippet of code below?
class Animal {}
class Dog extends Animal {}
public class DogTest {
public static void main(
String[] args) {
Animal animal = new Animal();
if (animal instanceof Dog) {
Dog d = (Dog) animal;
}
}
}
This code compiles and run fine, however, if you omit the instanceof
test it gives a ClassCastException.
Thanks in advance.