Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • I prefer this solution to Class.isAssignableFrom() Commented Jan 9, 2014 at 22:12
  • @MikeB, is there any particular reason why? Even after reading this question (stackoverflow.com/questions/3949260/…) I am still having trouble understanding why one is preferable over the other Commented Jan 9, 2014 at 22:42
  • @ryvantage, it's mainly a personal preference. The only real advantage that I'm aware of is you don't have to worry about a NullPointerException. Commented Jan 10, 2014 at 14:40
  • @MikeB, I think then you have the wrong preference. Because with isAssignableFrom you have to invoke the getClass() method of your parameter, which throws a NPE if it is null. isInstance is the method that won't throw a NPE. Commented Jan 10, 2014 at 16:42
  • @ryvantage Yes, I said I preferred this solution (the one in the answer this comment is below), which is c.isInstance(e) for exactly the reason you point out. Commented Jan 10, 2014 at 21:56