java - Morbid use of constants -


why should write (as collegue says):

import static org.apache.commons.lang.math.numberutils.integer_one; if (myintvariable == integer_one) { ... } 

instead of:

if (myintvariable == 1) { ... } 

?

i know use of constants recommended think value of numberutils.integer_one never change! write 1.

you should not. integer_one name no more meaningful 1. if value has other meaning (for example, month in year), using constant (like calendar.february) make code clearer.

i can guess constant in commons math library created in java 1.4 when there no integer cache , autoboxing, had sense in terms may reuse same integer object (not primitive int) in different places save memory. added performance reasons, not code clarity. it's obsolete: if need integer object, can use integer.valueof(1) or implicit autoboxing , cached one.


Comments