Thursday, 10 December 2015

Difference between Array and ArrayList in Java

Difference between Array and ArrayList is one question you may come across in a Java technical interview. Though performance wise both Array and ArrayList may give almost similar performance but functionality wise they both may be used in quite different scenarios.

This question may be asked while asking some Java Collections interview questions in order to test the knowledge of the candidate about the ArrayList being dynamic and the performance difference if any.

Array Vs ArrayList

  • First and the most important difference is Array is static and can't be resized once declared.
    Whereas ArrayList is dynamic and that is why also known as dynamic array. ArrayList also 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.
  • Array can store both primitive types as well as Objects whereas ArrayList can only store objects. Though Autoboxing has blurred that difference, but the important point to note that in case of ArrayList any primitive data type is still wrapped and stored as an Object.
    In case of Array
    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 -
    Employee[] employees = new Employee[5];
    In case of ArrayList if you want to store integers then you have to do this -
    List<Integer> myList = new ArrayList<Integer>();
  • Difference number 2 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.
    As Exp.
    Object[] names = new String[3];
    names[0] = 12;
    This will throw ArrayStoreException.
    Where as this
    String[] names = new String[3];
    names[0] = 12;
    Will throw compile time error "Type Mismatch".

    In case of ArrayList, generics brought the much needed type safety which, as shown above, is not required for Array.
    So, if a list is needed which should only Integers, it should be defined as,

    List<Integer> myList = new ArrayList<Integer>();

  • Performance wise both Array and ArrayList are almost similar as ArrayList also uses array of Objects internally. But there is some overhead in case of ArrayList in case of resizing the array.
  • 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.
    As exp. 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

  1. Difference between ArrayList and Vector in Java
  2. Difference between ArrayList and LinkedList in Java
  3. Difference between HashMap and HashTable in Java
  4. fail-fast Vs fail-safe iterator in Java
  5. Difference between Comparable and Comparator
  6. HashSet Vs LinkedHashSet Vs TreeSet in Java
  7. Java Collections interview questions

You may also like -

No comments:

Post a Comment