Java LongCache returns wrong value

227 Views Asked by At

Long variables in my online application get wrong value after runing sometime. I found this happens when my code autoboxing primitive long value to Long object implicitly. I know that for long value -127 to 128 java returns cached Long objects when program returns primitive long numbers. After some debugging, I see LongCache saves wrong value for some index. In my screenshot you can see that cache index 128 stores value 38 which should be 0; index 165 stores 38 which should be 37. From source code, LongCache is populated in static block during bootstrap. And there is no getter setter to update this cache. I can't think of a way how this could happen. The LongCache values become correct after restarting application. Has anyone ever encountered this issue? Thanks. My program is in Java 11.

128 index

165 index

0

There are 0 best solutions below