In java for a comparison in an if statement, I wrote
if (x == 1)
and got a comment in code review to use NumberUtils.INTEGER_ONE instead of 1. I was wondering what benefit does it actually add to the code.
In java for a comparison in an if statement, I wrote
if (x == 1)
and got a comment in code review to use NumberUtils.INTEGER_ONE instead of 1. I was wondering what benefit does it actually add to the code.
NumberUtils.INTEGER_ONEcomes probably from commons-lang.In
commons-lang, it is defined as :In
commons-lang3, it is defined as :The first version doesn't use the internal integer cache (as didn't exist yet) while the second version takes advantage of it.
Now, whatever the version you are using, it doesn't really matter for your question as you compare integer values and you don't assign or create integer value (case where the cache could make more sense).
Suppose you are using it in this way :
If
xis a primitive, it is not very efficient as it will produce an unboxing operation to convertNumberUtils.INTEGER_ONEto a1int primitive.If
xis an object, it is not a good idea either asIntegerobjects should be compared withequals()orintValue().