Explain the syntax of Collections.emptyList()

后端 未结 5 1859
故里飘歌
故里飘歌 2020-12-24 01:35

I just studied about generic programming, the List interface, and ArrayList, so I can understand the statement below.

Arra         


        
5条回答
  •  醉话见心
    2020-12-24 02:03

    What is Collections? Why isn't it Collections or Collections?

    Collections is a JDK class.

    This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends.

    It's not generic and cannot be instantiated.

    Why is placed before the method name emptyList?

    Collections#emptyList is a generic method. Here, we are explicitly specifying a type argument, String.

    (Isn't emptyList() correct for Generic?)

    No, in Java, generic type arguments for methods come before the method name.

    What does the statement mean?

    We are invoking the static emptyList method and assigning its return value to a variable of type List.

提交回复
热议问题