What are the List or ArrayList declaration differences in Java?

后端 未结 3 1134
挽巷
挽巷 2020-12-18 20:11

I am new to Java. I want to know the difference between:

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

and

ArrayLis         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-18 20:45

    The first one is only valid since Java 7, and is the equivalent of

    List list = new ArrayList();
    

    It's just less verbose.

    Same for the third one, which is equivalent to

    ArrayList list = new ArrayList();
    

    and thus strictly equivalent to the second one.

    You should prefer the first one, for the reasons mentioned in the answers to the following question: List versus ArrayList as reference type?

提交回复
热议问题