Instatiation of immutable objects
Creating new instances of immutable primitive type wrappers (such as
Number subclasses and
Booleans) wastes the memory and time needed for allocating new objects. Static
valueOf() method works much faster than a constructor and saves the memory, as it caches frequently used instances.
It is guaranteed that two
Boolean instances and
Integers between -128 and 127 to be pre-cached, thus you definitely should not use the constructor to instantiate them.
Integer i = new Integer(0); Boolean b = new Boolean(true);
Integer i = Integer.valueOf(0); Boolean b = Boolean.valueOf(true); // or Boolean.TRUE