tags:

views:

118

answers:

4

I was asked this in an interview. Not counting the time it takes to execute the method body. Any ideas?

+5  A: 
erickson
There is nothing like instanceof in a method dispatch. The instance already knows its class.
josefx
@josefx - Invoking a method in a static language like Java isn't as simple as looking to see whether a method with a matching signature exists in the target. Type information is also consulted; in fact, if the correct method signature exists in the target, but the qualifying type has been modified to remove the method declaration, a `NoSuchMethodError` will still be raised. The type information also controls accessibility in Java.
erickson
+1 Good point. Method lookup should still be simpler than instanceof since it can ignore Interfaces. But the speed of these lookups depends on the implementation, O(n) for crawling the parent classes and O(1) for a flat copy (vtable?)
josefx
+8  A: 

My answer to this question would be "I don't care". If I was having a problem with an application that I was writing and I suspected instanceof to be the cause, then I would profile to see if it really was the cause, but I wouldn't go through rewriting large chunks of code just on a "hunch".

Dean Harding
When I ask a technical question in an interview, I'm not looking for truisms like "premature optimization is the root of all evil." I do ask them to see if the programmer knows how things work "under the hood." Only programmers who care, and bother to find out, what's under the hood can fix bugs when things don't work as advertised.
erickson
@erickson: if you want to know whether the candidate knows how `instanceof` works under the hood, ask "how does `instanceof` work under the hood?"
Dean Harding
I'd learn that and a lot more about a candidate by asking the question as it was framed to the OP.
erickson
@erickson - Exactly, when the interviewer asks to how would I sort a collection from sratch, I cannot say use the built-in method provided in the api.
fastcodejava
+1 .No need to know the performance difference. IMHO overuse of instanceof or isType() methods is a sign of bad design.
josefx
+2  A: 

First, I would guess the method call, but only by a hair, since determining class instance relationship is actually fairly complicated involving not only the full class name but also the class loader that loaded it.

Second, measure and see, on the target hardware and JVM, then be prepared for the results to change with the next JVM version.

Third, who the heck cares, unless you are going to design an architecture that fundamentally depends on checking instances at a very very high rate. In any system of reasonable complexity, this is not going to even remotely be a factor... but that's just my 2 cents, and I am usually very predisposed toward giving thought to the performance of specific coding styles.

Fourth, the method call style is usually better OO than checking types, but one would have to see the system in context to be sure this is so.

Software Monkey
+1  A: 

I don't really understand this question... So the guy interviewing you wanted to see if you know whether an instanceof keyword would be faster than calling a random method? First of all I'd say say as it's a keyword so I'd figure that the JVM would have less problems with it than with a method you declared. But then I'd say that I don't really care since I don't really like to rely on instanceof and I really use it only when I'm forced to. I mean if I have to make a piece of code where I rely on something like "if T instanceof Z else ..." then I really take a minute or two to think whether I'm doing something wrong.

Zenzen