The difference between Array and ArrayList in Java is one question you may come across in Java technical interviews. While both structures are used to store data, they serve different purposes and have distinct characteristics. Understanding these differences is crucial for writing efficient and maintainable Java code
In this post we'll see some of the differences between ArrayList and Array in terms of how they are initialized and the performance they give.
Array Vs ArrayList in Java
- Fixed vs Dynamic Size
Array: Once declared, an Array has a fixed size and cannot be resized. ArrayList: An ArrayList is dynamic, often referred to as a "dynamic array". ArrayList uses array of Object internally, but it has the logic to keep growing the size of the array as and when previous size is not able to fit in the number of elements stored in the ArrayList.Refer: How ArrayList works internally in Java to know more about the internal implementation of ArrayList.
- Data Types Stored
Array: Can store both primitive types as well as objects.
ArrayList: Can store only objects. With autoboxing and unboxing introduced in Java 5, primitives are automatically wrapped into their corresponding wrapper classes- (e.g., int -> Integer).For example, if you want an array of primitive type int-
int[] intArray = new int[3];
Or, if you have a class Employee and you want an array of size 5 to hold 5 Employee objects then-
Employee[] employees = new Employee[5];
In case of ArrayList if you want to store integers then you have to do this, note the use of wrapper class Integer-
List<Integer> myList = new ArrayList<Integer>();
- Difference number 2 between array and ArrayList also indicates one more difference, which is about "type safety". Since Array knows the type of the data which it can hold so it will give compiler error "Type Mismatch" or
"ArrayStoreException" if it is not able to resolve it at run time. For example following code throws ArrayStoreException.
Object[] names = new String[3]; names[0] = 12;
Where as following code throws compile time error "Type Mismatch".String[] names = new String[3]; names[0] = 12;
In case of ArrayList, generics brought the much needed type safety which, as shown above, is not required for Array as type of elements stored in the array is specified at the array creation time itself, trying to store element of any other type will result in ArrayStoreException.
If a list, which stores only Integers, is needed it should be defined as-
List<Integer> myList = new ArrayList<Integer>();
- Performance Comparison
Array: Since its size is fixed, there is no overhead of resizing. Memory usage is more efficient, especially when storing primitives, as they are not wrapped into objects.
ArrayList: Internally backed by an array of objects, it offers dynamic resizing. When the internal array reaches capacity, a new larger array is created and elements are copied over, introducing overhead during expansion. Additionally, because ArrayList stores only objects, memory consumption is higher compared to arrays storing primitives - Array has length variable which gives the length of the array. Note that length attribute
denotes the length of the array at the time of declaration.
For example, If an array is declared like this-String[] names = new String[3]; names[0] = "Java";
Then length var will always be 3 even if array names has only one value.In case of ArrayList, size() method is used and it will give the size as the current number of elements in the ArrayList.
That's all for this topic Difference Between Array And ArrayList in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like-
No comments:
Post a Comment