My question is, since new Test()
is neither sub class of String
or it is String itself then why instanceof
compilation fails ? Shouldn't it return false.
public class Test{
public static void main(String[] args) {
//Compiles fails
System.out.println(new Test() instanceof String);
//compiles fine but run time class cast exception.
Test = (Test) new Object();;
}
}
Now i have edited my post, So this is what i really want to know why is this difference. Why compilation not failing on casting although it should