Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Performance of instanceof vs Class.isInstance()

807589Oct 8 2008 — edited Oct 10 2008
In a fairly performance critical part of an application that I'm working on, I have an option of using either the instanceof operator, or the Class.isInstance() method.

I can use either approach, but the latter makes the code a bit cleaner/more elegant

If we assume that the design for either approach will have the exact same performance, aside from the instance check, should I prefer one over the other?


To make it more concrete, I am wondering if one of these will be more perform better than the other:
public boolean isAString(Object o) {
   return o instanceof String;
}
public boolean isAString(Object o) {
   return String.class.isInstance(o);
}
And no, I don't actually plan on using this actual method.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 7 2008
Added on Oct 8 2008
9 comments
3,015 views