Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > instanceof vs class

Reply
Thread Tools

instanceof vs class

 
 
hilz
Guest
Posts: n/a
 
      07-22-2004
Hi,

If i have an object o, is there a difference bewten the two tests below? If
so, is one of them more efficient than the other?

if(o instanceof SomeClass)

if(o.getClass()== SomeClass.class)


thanks
hilz.



 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      07-22-2004
"hilz" <(E-Mail Removed)> writes:
>if(o instanceof SomeClass)


Here, SomeClass might also be a class extended by the class of
the object o, or an interface implemented by that class, or an
interface implemented by a class extended by the class of o,
or an interface extending an interface that is ...

>if(o.getClass()== SomeClass.class)


Here, not.

 
Reply With Quote
 
 
 
 
hilz
Guest
Posts: n/a
 
      07-22-2004

"Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)-berlin.de...
> "hilz" <(E-Mail Removed)> writes:
> >if(o instanceof SomeClass)

>
> Here, SomeClass might also be a class extended by the class of
> the object o, or an interface implemented by that class, or an
> interface implemented by a class extended by the class of o,
> or an interface extending an interface that is ...
>
> >if(o.getClass()== SomeClass.class)

>
> Here, not.
>


Thank you Stefan.
Now if i guarantee that i am always testing against either
A) the same class as my object
or
B) a totally different class that is neither a super class of my object nor
an interface that my object implements

in this case, will the two tests be the same?
if so, then does one of them perform better that the other?
I kind of remember reading somewhere that "instanceof" has some overhead,
which makes it slower that the other option.
can anyone confirm that?

thank you
hilz.


 
Reply With Quote
 
Michael David Pedersen
Guest
Posts: n/a
 
      07-22-2004
Hi,

"hilz" <(E-Mail Removed)> wrote in message
news:vKVLc.6261$(E-Mail Removed)...
> Hi,
>
> If i have an object o, is there a difference bewten the two tests below?

If
> so, is one of them more efficient than the other?
>
> if(o instanceof SomeClass)
>
> if(o.getClass()== SomeClass.class)


I believe "o.getClass() == SomeClass.class" will be true exactly if o is an
instance of SomeClass. On the other hand, "o instanceof SomeClass" will also
be true if o is an instance of a subclass of SomeClass.

Regards,
Michael.


 
Reply With Quote
 
hilz
Guest
Posts: n/a
 
      07-22-2004

"Will Hartung" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "hilz" <(E-Mail Removed)> wrote in message
> news:7BWLc.22063$(E-Mail Removed)...
> > Now if i guarantee that i am always testing against either
> > A) the same class as my object
> > or
> > B) a totally different class that is neither a super class of my object

> nor
> > an interface that my object implements
> >
> > in this case, will the two tests be the same?
> > if so, then does one of them perform better that the other?
> > I kind of remember reading somewhere that "instanceof" has some

overhead,
> > which makes it slower that the other option.
> > can anyone confirm that?

>
> Yes, a direct comparison using == will be faster than instanceof, but only
> because instanceof will walk the class->superclass chain of the tested
> object rather than simply fail.
>
> For example:
>
> public boolean instanceOf (Object o, Class c)
> {
> Class c2 = o.getClass();
>
> while(c2 != null) {
> if (c == c2) {
> return true;
> }
> c2 = c2.getSuperclass();
> }
> return false;
> }
>
> So, obviously instanceof will be slower.
>
> But why even worry about the speed of instanceof?
>
> Also, note:
>
> x = (SomeClass)y;
>
> Incurs the same cost as instanceof.
>
> So simply "yes it's faster", but I sure wouldn't ever worry about it

unless
> some profiler pointed it out as a smoking gun.
>
> Regards,
>
> Will Hartung
> ((E-Mail Removed))
>
>


Thanks Will.
The code you provided was very helpful making me understand the difference.
now i can make my decision based on that.
this comparison is going to be used at a very low lever where it will be
repeated a huge number of times, and such a difference in bahavior will make
a difference in the performace of the application.

i definately need the direct class comparison using the == operator.

thank you very much.
hilz


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-22-2004
On Thu, 22 Jul 2004 20:48:27 GMT, "hilz" <(E-Mail Removed)> wrote or
quoted :

>if(o instanceof SomeClass)
>
>if(o.getClass()== SomeClass.class)


They will give different results if the classes are Dog and Dalmatian.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Will Hartung
Guest
Posts: n/a
 
      07-22-2004

"hilz" <(E-Mail Removed)> wrote in message
news:7BWLc.22063$(E-Mail Removed)...
> Now if i guarantee that i am always testing against either
> A) the same class as my object
> or
> B) a totally different class that is neither a super class of my object

nor
> an interface that my object implements
>
> in this case, will the two tests be the same?
> if so, then does one of them perform better that the other?
> I kind of remember reading somewhere that "instanceof" has some overhead,
> which makes it slower that the other option.
> can anyone confirm that?


Yes, a direct comparison using == will be faster than instanceof, but only
because instanceof will walk the class->superclass chain of the tested
object rather than simply fail.

For example:

public boolean instanceOf (Object o, Class c)
{
Class c2 = o.getClass();

while(c2 != null) {
if (c == c2) {
return true;
}
c2 = c2.getSuperclass();
}
return false;
}

So, obviously instanceof will be slower.

But why even worry about the speed of instanceof?

Also, note:

x = (SomeClass)y;

Incurs the same cost as instanceof.

So simply "yes it's faster", but I sure wouldn't ever worry about it unless
some profiler pointed it out as a smoking gun.

Regards,

Will Hartung
((E-Mail Removed))


 
Reply With Quote
 
Carl Howells
Guest
Posts: n/a
 
      07-22-2004
hilz wrote:
> Thanks Will.
> The code you provided was very helpful making me understand the difference.
> now i can make my decision based on that.
> this comparison is going to be used at a very low lever where it will be
> repeated a huge number of times, and such a difference in bahavior will make
> a difference in the performace of the application.
>
> i definately need the direct class comparison using the == operator.


Argh. I think that's wrong.

First, I think you're wrong about how much it matters. I'm betting that
doing it either way will result in less than a .5% speed difference in
the overall application. You can make up for more than that by doing
things better at a higher level.

Second, I'm not even convinced Will Hartung is correct about which one
is faster. I'm very certain that if you move between different versions
and vendors of the JVM, you will get differing results in any testing there.

Third, I think having to perform either test indicates a big design
flaw, which will have more of an impact on your performance than either
test ever could.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-22-2004
On Thu, 22 Jul 2004 15:32:04 -0700, "Will Hartung" <(E-Mail Removed)>
wrote or quoted :

>Yes, a direct comparison using == will be faster than instanceof, but only
>because instanceof will walk the class->superclass chain of the tested
>object rather than simply fail.


On the other paw instanceof is a single JVM instruction, and may be
highly optimised or short circuited via optimisation versioning. But,
in theory it SHOULD take longer than a single class compare.

As always MEASURE if it matters.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
hilz
Guest
Posts: n/a
 
      07-22-2004
Got it!
Thanks Roedy,
by the way.... how did that homework of yours go?! did you receive anything
by DHL the next day?!!!

just kidding!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check if object is an instanceof a class, but not a sub-class? laredotornado Java 47 06-29-2010 07:05 AM
checking class type alternative to instanceof Timasmith Java 3 11-13-2006 10:48 PM
instanceof NOT (always) bad? The instanceof myth. dmx_dawg@hotmail.com Java 21 07-20-2006 07:06 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Re: Dumb instanceof question Paul Tomblin Java 35 07-27-2003 07:31 PM



Advertisments