multiple checks in a if statement
In java is there a faster way of doing this?
开发者_如何学Cif (keyCode != 66 && keyCode != 8 && keyCode != 21 && keyCode != 22) {
}
keyCode is an int.
Faster? Is it too slow for you? Don't play optimizer. Write readable code and leave microoptimizations to the optimizer. Premature optimization is the root of all evil
Edit after josh's comment:
If you have really many of them, put them in a container (such as a set or an array) and find
keyCode
in it. If you found it, then your condition is false. Otherwise it's true.
As per Dave's comment:
if(!MyCodesSet.Contains(keyCode)){
}
Yes, the switch
statement will translate to direct jumps. It is however possible that the JIT does that to your code anyway, so you have to try it out if switch
is actually faster.
精彩评论