JavaAntiPatterns

Collection of bad coding practices

Posts Tagged ‘constructor

Instatiation of immutable objects

with 2 comments

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.

Read the rest of this entry »

Written by Alex

November 22, 2007 at 3:14 pm

Posted in Objects

Tagged with ,