I am using Code Pro to review my application code and the tool reported back with the message:
Warning: equality test with boolean literal
For this code:
boolean valid;
if(valid == true)
which can be fixed by using:
if(valid)
I have 2 questions on my mind:
- Is it just a good coding practice ?
- Is there any other benefit in terms of memory optimization or performance optimization ?
For the more direct boolean expression:
The following byte code is generated:
Whereas with the expanded comparison:
The following byte code is generated:
I doubt
ifeqandif_icmpnediffer in execution speed, so the additional cost ofif(valid == true)is really just the extra constant value, which is negligible.To summarize, there is real no performance difference, and CodePro is flagging your code as a best practice alone.