Monday, January 1, 2024

Difference Between Array And ArrayList in Java

Difference between Array and ArrayList in Java 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 are used in quite different scenarios.

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

  1. 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.
  2. Refer: How ArrayList works internally in Java to know more about the internal implementation of ArrayList.

  3. Array can store both primitive types as well as objects whereas ArrayList can store only objects. Though Autoboxing and Unboxing has blurred that difference, but the important point to note here is that in case of ArrayList any primitive data type is still wrapped and stored as an Object.

    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>();
  4. 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>();
  5. Performance wise both Array and ArrayList are almost similar as ArrayList also uses array of Objects internally and both are index based. But there is some overhead in case of ArrayList if there are more elements and the internal array has to be resized.
    Since ArrayList stores only objects so memory usage is also more than the array.
  6. 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

  1. Difference Between ArrayList And LinkedList in Java
  2. Difference Between ArrayList And Vector in Java
  3. Difference Between ArrayList And CopyOnWriteArrayList in Java
  4. HashSet Vs LinkedHashSet Vs TreeSet in Java
  5. Java Collections Interview Questions And Answers

You may also like-

  1. Difference Between Comparable and Comparator in Java
  2. TreeMap in Java With Examples
  3. Polymorphism in Java
  4. Method overriding in Java
  5. Race condition in Java multi-threading
  6. Java ThreadLocal Class With Examples
  7. Lambda expression and exception handling
  8. Dependency Injection in Spring Framework

No comments:

Post a Comment