Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does instanceof operator generate a lot of overhead ? Why? [duplicate]

I have a colleague here in my project, who is deeply against the use of instanceof operator, because it "generates a lot of overhead", what is the reason for that? Is it true?

Is there another way to check the type of the Object instead of using it?

Because I find it very useful in some occasions.

like image 527
Cristiano Fontes Avatar asked Feb 11 '11 19:02

Cristiano Fontes


1 Answers

It does generate some overhead, combined with the subsequent casting. With recent version of Java the overhead has decreased. But anyway that's microoptimization - i.e. you should not worry about it in the general case.

The real argument against instanceof is that in many cases there are better OOP ways to achieve the desired behaviour.

like image 190
Bozho Avatar answered Nov 14 '22 07:11

Bozho