Don't confuse size and capacity. These are different.
Size defines the number of elements this very List contains.
Capacity term is more relevant to List internals or more precisely, how it allocates space for the elements it is about to hold. ArrayList storage implemented using an array. When you create an ArrayList, by default it allocates 0 capacity, which in turn creates zero length array internally. So, that parameter you were asking, defines what length array is created during ArrayList instantiation.
Campbell Ritchie pointed out that I was mistaken about the initial capacity of an ArrayList. It is actually 10. Originally I thought it creates capacity of 0, and only when the first element gets attempted to be added, capacity gets increased to 10.
Liutauras Vilda wrote:Campbell Ritchie pointed out that I was mistaken about the initial capacity of an ArrayList. It is actually 10. Originally I thought it creates capacity of 0, and only when the first element gets attempted to be added, capacity gets increased to 10.
I have gone through the code last night so If you create ArrayList with non parameter constructor then initially It just assigns an empty static array of type Object. When you add an element then creates an array of Object with default size 10 else It wont create array of size 10 unnecessarily occupying space.
In the source code of ArrayList It has comment probably of authors which says ( I could be wrong but find It confusing and misleading since DEFAULTCAPACITY_EMPTY_ELEMENTDATA; is an empty array )
But If you go through the code in source code then you will see It creates an empty one dimensional array of type static Object when this class is loaded.
If creates an object of ArrayList with no parameter constructor then It just assigns above already created empty static array of Object to It's instance variable of type transient Object elementData; to use It further.
Hence here capacity at the beginning is 0. The moment you add an element using add() method It creates an array of having capacity 10 i.e. default capacity
If you create ArrayList with parameter constructor passing initialCapacity then creates an array of capacity equal to the value passed as initialCapacity.
It's really logical to create an empty static array when class is loaded and initially assign that same array to instance array variable of all objects of ArrayList created with non parameter constructor.
It creates new array with default capacity 10 only when add method is invoked.
This reduced fruitless memory consumption, how? Suppose If they had created array of capacity 10 when you just create object of ArrayList with non parameter constructor then
above code need memory for 100 arrays of type Object having capacity 10
but we haven't even used any of them yet consumed so much memory so they use only one static array at the beginning once you add an element then creates array of default capacity 10.
In previous post I wrote:I could be wrong but find It confusing and misleading since DEFAULTCAPACITY_EMPTY_ELEMENTDATA; is an empty array
Liutauras Vilda wrote:Can we state that JavaDocs are wrong, or at least misleading?
JavaDocs wrote:public ArrayList()
Constructs an empty list with an initial capacity of ten.
Since I found following information now I'm thinking both are correct in different views ( Couldn't find exact word to use here )
Googled more about ArrayList() default capacity almost all of them says 10 but I came to know a new and interesting thing is Lazy initialization
Lazy initialization Wikipedia wrote:In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed.
Hence Javadoc explanation might have considered Lazy initialization.