1

Are there any major disadvantages of initializing a Java collection, e.g. ArrayList at the time of declaration such as :

List<String> strList = new ArrayList<String>();

The main purpose is to avoid the clutter of null checks while retrieving the elements.

Thanks

3
  • 1
    Some more information about the situation would be useful Commented Nov 12, 2013 at 18:50
  • 2
    What are you comparing this declaration to? The performance of this declaration is better compared to ...? Commented Nov 12, 2013 at 18:50
  • Any asnwer about arraylists can be marked as an answer for this question. be more clear. Commented Nov 12, 2013 at 18:53

3 Answers 3

3

In general, the only disadvantage is that you may end up with a whole bunch of collections doing nothing and have unnecessarily done work to create the collection.

My personal preference is to assign a valid collection always unless there's a definite reason to avoid the memory and initialization overhead, such as within loops and other possible performance considerations. And if I was always ensuring the objects were initialized to non-null I would not then do redundant null-checks before using the collections.

The caution I would add is to make sure if you do use the collection that you use it and don't, in later code, replace it with an entirely new collection (that would indicate a probable design flaw). To enforce this you can declare the collection as final.

Sign up to request clarification or add additional context in comments.

2 Comments

If the collection reference is never reassigned, I would declare it final as well. Then you know it's initialized and never null.
that makes perfect sense.
1

It depends on the use case:

  • Method body declaration - always ok.
  • Static field declaration - should always be ok.
  • Field declaration. There are two popular cases - when you always need it initialized or when it might be null. In the first case I make it final and prefer initializing in a constructor. If it might be null, I add a lazy getter and annotate it with @Nullable like this:
@Nullable
List<String> strList;

...

public final List getList(){
    if(strList == null){
        strList = new ArrayList();
    }
    return strList;
}

This technique helps me to eliminate ambiguity of field states.

Comments

0

You should almost always initialize right away. Just because it's initialized doesnt mean you shouldnt do null checks anymore. Creating the object and an if check have almost no effect on performance. Only when you're creating a list that has to copy objects from other collections or arrays

1 Comment

This advice doesn't scale. Create "an" object has no material effect on performance; creating a million objects in a tight loop will, not only for the allocation and initialization but with the additional load on GC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.