Design questions aside, what performs faster on modern JVMs?
foo instanceof Bar
or
Bar.class.isInstance(foo)
Why?
Design questions aside, what performs faster on modern JVMs?
foo instanceof Bar
or
Bar.class.isInstance(foo)
Why?
foo instanceof Bar
should be faster.
You can use Bar.class.isInstance(foo)
if it's not clear at compile time which class you have.
consider the following:
void test(Object o1, Object o2) {
o1.getClass().isInstance(o2);
}
In this exsample the JVM decides at runtime which class calls the method.
With instanceof
this is not possible.
So if you know the class at compile time you should use instanceof