In Integer class
, there is private static class IntegerCache
.
What is the use of this class?
What are the benefits of it while using Integer
?
Values between -128 and 127 are cached for reuse. This is an example of the flyweight pattern, which minimizes memory usage by reusing immutable objects.
Beyond being an optimization, this behaviour is part of the JLS, so the following may be relied upon:
Integer a = 1;
Integer b = 1;
Integer c = 999;
Integer d = 999;
System.out.println(a == b); // true
System.out.println(c == d); // false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With