gotchajavaCritical
What is the difference between instanceof and Class.isAssignableFrom(...)?
Viewed 0 times
andclassbetweenthedifferenceisassignablefrominstanceofwhat
Problem
Which of the following is better?
or
The only difference that I know of is, when 'a' is null, the first returns false, while the second throws an exception. Other than that, do they always give the same result?
a instanceof Bor
B.class.isAssignableFrom(a.getClass())The only difference that I know of is, when 'a' is null, the first returns false, while the second throws an exception. Other than that, do they always give the same result?
Solution
When using
instanceof, you need to know the class of B at compile time. When using isAssignableFrom() it can be dynamic and change during runtime.Context
Stack Overflow Q#496928, score: 559
Revisions (0)
No revisions yet.