What is the purpose of List?

前端 未结 4 552
清歌不尽
清歌不尽 2021-02-05 01:01

I didn\'t even know this was doable, but I saw while perusing some code online a method with a signature like this:

public List read( ... )
         


        
4条回答
  •  失恋的感觉
    2021-02-05 01:29

    List is weird. It can only have null elements, since you can't create an object of type Void. I don't think there is a practical use for such a thing.

    Void is part of java.lang. It's not a special keyword or anything. It's a "pseudo-type" (according to the docs) used to as a place-holder to represent the Class object corresponding to void, as in Class. From the docs for Class:

    The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

    The Void class exists mainly for the sake of the last part of this, so you can write:

    Class voidType = void.class; // == Void.TYPE
    

    just like you can write:

    Class intType = int.class; // == Integer.TYPE
    

提交回复
热议问题