Is is possible to check whether given Object item can be casted to some class? Is there any method which doesn’t throw an exception?
Share
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.
Yes,
Class.isInstance(Object)and the relatedClass.isAssignableFrom(Class)Example:
Edit: You said “method” so I assumed you meant an API method, but if you know the types at compile-time then you can simply use
(see also What is the 'instanceof' operator used for?)