I was asked this in an interview. Not counting the time it takes to execute the method body.
Any ideas?
I was asked this in an interview. Not counting the time it takes to
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’d want to profile it to quantify the difference, but I would guess
instanceofis faster.Think about it…
booleanReturnMethod()is an instance method (if it were static, you’d already know the class), so the first step in method dispatch would be to determine what class of object it is. That’s like aninstanceofright there.But then the actual method body has to be located, and the invocation has to be set up. Even if you discount the method body itself, that preparation takes some additional time.