What is the difference between Array and ArrayList is the quite common question for the Java interview and for the beginners who are learning Java programming. I have started writing the Java best practices series, this is one of the post for clearing the air on misconception about popular Java classes Array and ArrayList.
1. Flexible Storage
One of the major difference between Array and ArrayList is that, Array has the fixed length and it can not be extended once it is initialized. Here is the simple construct for initializing the arrays which I have taken from Java Arrays Example:
int weight[ ]= new int[5]; weight[0]=36; weight[1]=42; weight[2]=24; weight[3]=10; weight[4]=50;
If you look at the above example, array variable weight
is initialized with the length 5, it can not accommodate more than 5 elements.
Whereas ArrayList can dynamically increase the size when new elements are added. Look at the following code:
ArrayList<String> list = new ArrayList<String>();
When you create the above object, the default size of 10 is internally assigned for storing the values. However, when you add the eleventh element, the ArrayList size will be extended to another 10 elements. This impacts the performance of your programming if it has to increase multiple times for accommodating more elements.
2. Generics
Arrays can not use the Generics programming introduced in Java 5. Array knows the type which it can hold, otherwise it will throw the ArrayStoreException
when you try to store the wrong data type.
Whereas one of the great advantages of the collections programming is to use the generics for specifying the type of the objects an ArrayList
can store. This provides the type-safety for your code at compile time itself.
3. Length / Size
Arrays provide the method length
for accessing the size of an array. Even though you have not stored any elements in the array, this method will always returns the capacity of the array.
ArrayList or any other collections APIs that implements List interface defines the method size
accessing the size of the object. Note that size
method of ArrayList returns only the number of elements currently stored, to know the maximum elements that can be stored in the ArrayList can be accessed using the method capacity.
4. Primitives and Objects
Arrays can be created for either primitives or objects. It can store primitive types as well as objects.
ArrayList can hold only the objects using the add
method. Since the Java 5 version, though you store the primitive types to an ArrayList, it silently converts (autoboxing in Java 5) the primitive value to corresponding wrapper class and stores it. All the collection APIs support only storing of the objects and not the primitive values.
5. Iterator
One of the greatest advantage of storing the objects in the collections API is use of iterator for iterating the elements. But, this feature is not available with the Arrays, we have to do the manual iteration using for loop or while loop constructs.
6. Performance
There is no difference in terms of performance between Array and ArrayList classes. They both offer the similar performance for storing and retrieving the objects. If you are storing the thousands of objects in your application, you can measure the performance difference between Array and ArrayList. ArrayList internally uses the Array for storing the objects.
If you are performing the resize() operation or automatic resizing of the ArrayList, the it will impact the performance.
Best Practice
The recommendation is only my opinion and not to be taken as universal best practice for the use of Array vs ArrayList in your projects. I would recommend using the ArrayList every where because it offers the lot of flexibility for storing the objects. If you want thread safety, choose Vector instead of ArrayList.