JavaAntiPatterns

Collection of bad coding practices

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.

Bad:

Integer i = new Integer(0);
Boolean b = new Boolean(true);

Good:

Integer i = Integer.valueOf(0);
Boolean b = Boolean.valueOf(true); // or Boolean.TRUE
Advertisements

Written by Alex

November 22, 2007 at 3:14 pm

Posted in Objects

Tagged with ,

2 Responses

Subscribe to comments with RSS.

  1. used in a majority of both over the counter and prescription acne treatments.
    This means that you might not be able to use waterproof sunscreen which means you will need to reapply sunscreen frequently if you plan on being in the water.
    Lastly, this product is great for those people who greatly suffer
    from excessively oily skin.

    opinie o derminax

    September 10, 2014 at 4:58 am

    • hahahahaha. man. someone has read your comment.

      douglas

      November 20, 2016 at 11:32 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: