The following compiles fine:
Object o = new Object();
System.out.println(o instanceof Cloneable);
But this doesn’t:
String s = new String();
System.out.println(s instanceof Cloneable);
A compiler error is thrown.
What is the problem?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A more blatant incarnation of your problem is the following:
This is specified in JLS 15.20.2 Type comparison operator
instanceof:That is, since this cast expression generates a compile time error:
so must this expression:
Your case is a bit more subtle, but the principle is the same:
Stringis a final classStringdoes not implementCloneable(Cloneable) aStringaString instanceof Cloneable