I can’t understand why in java there is a special array in the wrapper class, for Example Integer, where wrapper objects for primitives are stored: from -128 to 127. Why a new wrapper object not created for this range, like for other primitives? Does this array optimize the program so much? And why is the range 8 bytes, no more, no less? If someone is familiar with the history of the language, please explain this decision.
Asked
Active
Viewed 25 times
0
-
You should include a subsection of the code from the wrapper class, and also clarify which JDK we are referring to – Chris Maggiulli Oct 22 '22 at 04:44
-
2How many times in your program do you use small numbers, say 0, 1, 2, up to 10? My guess is: a lot. Now how many times do you store small numbers in collections, or use them as keys in a hashmap? Every single one of those uses would be an object allocation if not for caching. – Silvio Mayolo Oct 22 '22 at 04:44
-
So why java does not use 16 bytes? I often face with numbers more than 127. – Grey may Oct 22 '22 at 04:55
-
@ChrisMaggiulli It's actually in the Java Language Spec, not just specific JDK implementations. I'm in a phone, so I can't easily look up the exact chapter/section. – yshavit Oct 22 '22 at 05:24