简体   繁体   English

Java整数常数-拆箱

[英]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. 我只是为了常量顺序而依靠Apache NumberUtils进行更改。 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. 我正在考虑JVM包装器取消装箱,但是由于JVM默认在启动时(而不是在运行时)实习到最接近零(包括零)的前256个,因此我不确定。 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). 取消对Integer装箱的费用非常小,整数缓存的缓存范围是byte (所以为-128127 ,而不是最接近零的前256)。 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). 但是,成本很小,我希望您能找到最容易阅读的内容(不确定将0拼写对自己的可读性有帮助)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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