简体   繁体   中英

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM