How to interpret “public T readObjectData(… Class type)” in Java?

前端 未结 4 1080
不知归路
不知归路 2020-12-02 11:20

I have this Java code.

public  T readObjectData(ByteBuffer buffer, Class type) {
...
T retVal = (T) summaries;
return retVal;
4条回答
  •  抹茶落季
    2020-12-02 11:50

    The part is declaring a generic type argument T. If you were to omit this part, the compiler would likely complain that the type T doesn't exist.

    In this case, T serves as a placeholder for an actual type, which will only be determined when the method is actually called with non-generic type arguments.

    public  T readObjectData(...
            ^  ^
            |  + Return type
            + Generic type argument
    

提交回复
热议问题