What are the pitfalls of comparing Class instances using the equality operator?
boolean compareTypes(Class<?> clazz, Class<?> rootClazz) {
return clazz == rootClazz;
}
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.
No pitfalls really; it behaves just as you’d expect, if you expect the actual behavior 🙂 Besides, the
equals()method forClassobjects is just the inherited one fromObject, which uses the==operator anyway.The only surprising part is if the same class file is loaded by two different class loaders, you’ll get two separate class objects which will compare as
false. This is by design.