One of the new features in Java 9 is the condemnation of the constructor of wrapper objects. The only way to create new Wrapper objects is to use static methods valueOf(). For example, for objects, Integer Integer.valueOfimplements a cache for values from -128 to 127 and returns the same link every time you call it.
As the API for the class Integersays: "A static factory valueOf (int) is generally a better choice, as it is likely to give significantly better performance for space and time." and JLS says: “Given the value of the corresponding primitive type, there is usually no need to create new instances of these box classes. Recommended building alternatives are autoboxing or staticOf static factory. In most cases, autoboxing will work, so an expression whose type is primitive can be used in places where a drawer class is required.
But what happens to values outside this range? For example, Integer x = Integer.valueOf (456) is a new object every time a class is executed?
source
share