Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Lists: List Or ArrayList

Skar.2007May 3 2022

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

Comments

User_N115J

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.

1 - 1

Post Details

Added on May 3 2022
1 comment
107 views