Forum Stats

  • 3,816,904 Users
  • 2,259,249 Discussions
  • 7,893,591 Comments

Discussions

Lists: List Or ArrayList

Skar.2007
Skar.2007 Member Posts: 1 Red Ribbon

Java 8 | 18

Hello everyone

1° What is the functional difference between ArrayList and List? Or in other words, which one is better?

2° Why is a List declared using the ArrayList constructor: List myList = new ArrayList()? How is this possible if they are different Classes?

Thank you very much already

Answers

  • User_N115J
    User_N115J Member Posts: 3 Green Ribbon

    List is an Interface and not a class.

    ArrayList  is a class that implements the List interface.

    Interfaces and abstract classes can never be instantiated.

    Lets say you have a function that returns a collection of values, it is better to declare the return as List ;So when calling this function you aren't stuck with a concrete class like Arraylist.

    You will have the ability to use any class that implements the List interface.