Java Basics-List Interface-ArrayList/LinkedList Implementation Class

Take ArrayList as an example

Grammatical difference:

  1. List list = new ArrayList();
  2. ArrayList arrayList = new ArrayList();

From the interface point of view:

  1. List is an interface (abstract class) and cannot be instantiated;
    ArrayList can be instantiated for its implementation class.
  2. If it is declared as a List object, the custom and unique methods and properties in ArrayList cannot be used.
  3. Declaring it as a List object can standardize the code and facilitate code refactoring; it can accept ArrayList objects or LinkedList objects, and only one line needs to be changed when changing, and other methods and properties that use List objects can be retained.

Tips:
ArrayList<E> : Generic concept, E can be of any type. Confirmation cannot be changed.
Benefits: Improve safety, the compiler checks whether the type is safe; improve readability, avoid mandatory type conversion.

Guess you like

Origin blog.csdn.net/qq_32301683/article/details/108745640