PMD tells me
A switch with less than 3 branches is inefficient, use a if statement
instead.
Why is that? Why 3? How do they define efficiency?
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.
Because a
switchstatement is compiled with two special JVM instructions that arelookupswitchandtableswitch. They are useful when working with a lot of cases but they cause an overhead when you have just few branches.An
if/elsestatement instead is compiled into typicaljejne… chains which are faster but require many more comparisons when used in a long chain of branches.You can see the difference by looking at byte code, in any case I wouldn’t worry about these issues, if anything could become a problem then JIT will take care of it.
Practical example:
is compiled into:
While
is compiled into