Search code examples
javajvmintegerwrapperunboxing

Java Integer Constant - Unboxing


Given this code change:

int count = 0;

Replaced by:

int count = NumberUtils.INTEGER_ZERO;

I relied on Apache NumberUtils to change, just for the sake of constants order. What I wanted to know is if there is any drawback for performing this change. I'm thinking about JVM wrapper Unboxing but I'm not sure given that JVM interns the first 256 closest to zero (zero included) on startup by default, not on runtime. Could anyone point that out?


Solution

  • There is a very small cost to unboxing the Integer and the range that the integer cache caches is of byte (so -128 to 127, not the first 256 closest to zero). However, the cost is small enough that I would prefer whichever you find most readable (not sure that spelling out 0 really helps readability myself).