Why are constructors such as Integer(); Float(); Double(); Boolean(); deprecated? It’s recommended to use WrapperClassName.valueOf() but how does it differ? They return a new WrapperClass, I don’t seem to understand the point of it so can someone explain it to me?
Integer x = new Integer(4); // Deprecated since Java version 9, use Integer.valueOf(int)
>Solution :
Integer.valueOf caches small values.
If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.