假设我们有两种方法,如下所示:
public static <T> T genericReturn() { /*...*/ }
public static String stringReturn() { /*...*/ }
Run Code Online (Sandbox Code Playgroud)
在调用任何方法时,无论是否有任何要求,您都可以提供类型见证:
String s;
s = Internet.<String>genericReturn(); //Type witness used in return type, returns a String
s = Internet.<Integer>stringReturn(); //Type witness ignored, returns a String
Run Code Online (Sandbox Code Playgroud)
但是我根本没有在Java中看到任何实际用途,除非无法推断出这种类型(这通常表明存在更大的问题).此外,当它没有被适当使用时被简单地忽略这一事实似乎违反直觉.那么在Java中有什么意义呢?