0

I am wondering about the difference between these two options to create a list of string. Which one to use when creating a list in Java?

Option 1

List<String> list1 = new ArrayList<String>();

Option 2

ArrayList<String> list2 = new ArrayList<String>();
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Iris
  • 19
  • 2
  • the `List` is an interface of which implemented by `ArrayList`, `LinkedList`, etc. you should check the javadocs for more info. and a bit understanding in OOP for which one you should use (esp. inheritance stuff). – Bagus Tesa Jul 25 '22 at 06:40
  • The answer is "it depends". – Mark Rotteveel Jul 25 '22 at 06:41
  • Option 1 is the commonly used way, but it depends on the task. – LewoSoft Jul 25 '22 at 06:43
  • 1
    Always read *The Java Tutorials* before posting a question on the basics of Java. Oracle Corp publishes those online free-of-cost. You would have found: [*The `List` interface*](https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html). The second sentence answers your question. – Basil Bourque Jul 25 '22 at 07:01

0 Answers0