What does it mean when a generics function has two return types?

风格不统一 提交于 2020-01-17 07:59:06

问题


I am going through a tutorial and I found this particular code.

 private <V> V fromJson(HttpRequest request, Class<V> target) throws IOException {
    Reader reader = request.bufferedReader();
    try {
        return GSON.fromJson(reader, target);
    } catch (JsonParseException e) {
        throw new JsonException(e);
    } finally {
        try {
            reader.close();
        } catch (IOException ignored) {
            // Ignored
        }
    }
}

I notice that the fromJson function has two return types? I have the basic idea of generics and how it works. What I can't understand is how are two types specified and how will this function know which type to assign the value to when its called.


回答1:


No, there is only one return type. The <V> declares the V generic type parameter (it makes the method generic), and the V after that is the actual return type.

Here's more information on generic methods in Java.



来源:https://stackoverflow.com/questions/17604970/what-does-it-mean-when-a-generics-function-has-two-return-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!