0

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?

Sebastian Motavita
  • 355
  • 1
  • 3
  • 9
  • 4
    I don't understand why you would use `int count = NumberUtils.INTEGER_ZERO;` instead of `int count = 0;`. Are you expecting the value of zero to be altered in the future? – khelwood Dec 01 '18 at 15:34
  • 2
    @khelwood I have altered the value of zero; [pray I do not alter it further](https://www.youtube.com/watch?v=m4H4dIJmzU0&feature=youtu.be&t=41). – Elliott Frisch Dec 01 '18 at 15:37
  • 1
    Given the amount of confusion you have already experienced, imagine now that everyone reading the code is likely to have a similar delay in understanding why you didn't just use 0 and what the impact might be. – Peter Lawrey Dec 01 '18 at 16:00
  • int count = 0; is pretty straight forward. May i know the reason for you to consider using int count = NumberUtils.INTEGER_ZERO; ? – Aarish Ramesh Dec 03 '18 at 15:58

1 Answers1

2

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

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • That's great thanks. I think the unboxing only happens once, after that is cached in the IntegerPool. Note that I never said the first POSITIVE 256 integers, it's just another way to express that range. – Sebastian Motavita Dec 02 '18 at 20:13
  • And I still don't understand the relevance of the integer cache to *unboxing*. – Holger Dec 02 '18 at 22:40
  • @Elliott I don't understand how is int count = NumberUtils.INTEGER_ZERO more readable than int count = 0 – Aarish Ramesh Dec 03 '18 at 15:57