1

What is the difference between ArrayList al = new ArrayList(); and ArrayList al = new ArrayList(0)?

2
  • new ArrayList() is equivalent to (and probably implemented in terms of) new ArrayList(10) Commented Jan 12, 2011 at 6:48
  • May be a difference on memory allocation by JVM. Commented Jan 12, 2011 at 6:50

3 Answers 3

6
ArrayList(0) 

Empty list with the specified initial capacity. Hense none for 0

ArrayList() 

Empty list with an initial capacity of ten.

Please read the following: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

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

Comments

5

If you look on the API, it says, ArrayList() - Constructs an empty list with an initial capacity of ten.

ArrayList(int initialCapacity) - Constructs an empty list with the specified initial capacity.

1 Comment

in (open) JDK8, this seems to be not the case. It does say that it creates an initial capacity of 10, but in fact it creates a capacity of 0: grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/…
1

new ArrayList() gives you an array list with default initial capacity (how much memory is initially allocated from the ArrayList). new ArrayList(0) gives you an array list with zero initial capacity. As soon as an element is added to the list, capacity is allocated.

Comments

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.