Java List vs ArrayList

前端 未结 7 1732
有刺的猬
有刺的猬 2020-12-07 01:14

As a C++ oldtimer I have managed to solve my problem but I can not wrap my head around the underlying Java mechanisms here:

Vector x = new Vec         


        
                      
相关标签:
7条回答
  • 2020-12-07 01:44

    The List is an interface. You cannot create in instance of an interface using new operator. That's why the line List<Object> z = new List<Object>(); gives error. Only classes can be instantiated.

    0 讨论(0)
  • 2020-12-07 01:45

    List is an interface, somewhat like a class with some = 0 methods in C++. You can't instantiate it.

    But ArrayList<T> "inherits" List<T> (or in Java terms, implements it), so those references are assignment-compatible.

    0 讨论(0)
  • 2020-12-07 01:48

    List is an interface, you cannot initialize it. ArrayList implements List, so you can assign an ArrayList instance to a List variable.

    0 讨论(0)
  • 2020-12-07 01:51

    "Interface" is like a protocol that an object must comply with.

    0 讨论(0)
  • 2020-12-07 01:53

    List is an interface and an interface can't be instantiated.

    It's used to implement polymorphism. i.e. a reference of interface type can hold object of any class that implements it.

    List<Object> zzz = new ArrayList<Object>();
    

    it works, cause ArrayList implements List.

    0 讨论(0)
  • 2020-12-07 01:57

    List isn't class it's an Interface and you can't instantiate the interface object.

    ArrayList is the class which was implement the List interface so can able to instantiate the ArrayList object and assign to the List object

    0 讨论(0)
提交回复
热议问题