Take ArrayList as an example
Grammatical difference:
List list = new ArrayList();
ArrayList arrayList = new ArrayList();
From the interface point of view:
- List is an interface (abstract class) and cannot be instantiated;
ArrayList can be instantiated for its implementation class. - If it is declared as a List object, the custom and unique methods and properties in ArrayList cannot be used.
- 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.