Wildcard Type Mismatch (int) 编译错误,但不是 (String)
Wildcard Type Mismatch (int) compilation error, but not (String)
我已经阅读了几个与我的类似的问题,但我仍然不明白发生了什么,或者为什么它只发生在 (int) 而不是其他更复杂的类型上:
HashMap<?, ?> returncp = startAndWaitForJob("DSP_WF_" + operation, cp);
DSPResponseDTO dto = (DSPResponseDTO) returncp.get("RESPONSE_OBJECT");
String respDesc = (String) returncp.get("statusInfoResponseDescription");
int respCode = (int) returncp.get("statusInfoResponseCode");
编译错误:
[javac] int respCode = (int) returncp.get("statusInfoResponseCode");
[javac] ^
[javac] required: int
[javac] found: CAP#1
[javac] where CAP#1 is a fresh type-variable:
[javac] CAP#1 extends Object from capture of ?
已阅读的问题:
incompatible types and fresh type-variable
Bounded-wildcard related compiler error
Java: Wildcard Types Mismatch Results in Compilation Error
我不知道你的地图里有什么类型,但是这里
String respCode = (int) returncp.get("statusInfoResponseCode");
您将 get
调用的结果转换为 int
并将其分配给 String
。
如果是Integer
,使用
Integer respCode = (Integer) returncp.get("statusInfoResponseCode");
首先是什么CAP#1
我找到了这个 quote:
What on earth does "capture#337 of ?" mean? When the compiler encounters a variable with a wildcard in its type, such as the box parameter of rebox(), it knows that there must have been some T for which box is a Box. It does not know what type T represents, but it can create a placeholder for that type to refer to the type that T must be. That placeholder is called the capture of that particular wildcard. In this case, the compiler has assigned the name "capture#337 of ?" to the wildcard in the type of box. Each occurrence of a wildcard in each variable declaration gets a different capture, so in the generic declaration foo(Pair x, Pair y), the compiler would assign a different name to the capture of each of the four wildcards because there is no relationship between any of the unknown type parameters.
现在让我们回到消息CAP#1 extends Object from capture of ?
它认为 CAP#1
是 Object
的一个实例,更像是 class 扩展 class Object
.[=25 的一个实例=]
现在需要想象泛型在编译时被转换为强制转换。这意味着:
HashMap<?, ?> map = new HashMap();
int test = (int) map.get("");
大致相当于:
HashMap<?, ?> map = new HashMap();
int test = (int) (Object) map.get("");
不可能(或编译器不允许)将 Object
转换为 'int'。
当然,这个Object
是用来表示简单的
但是完全可以将 Object
转换为 'String' 或 'Integer'。
所以你可以做的是:
HashMap<?, Integer> returncp = startAndWaitForJob("DSP_WF_" + operation, cp);
在那种情况下,您甚至不需要投任何东西 (see)。
但如果由于任何原因无法做到:
int respCode = ((Integer) returncp.get("statusInfoResponseCode")).intValue();
我已经阅读了几个与我的类似的问题,但我仍然不明白发生了什么,或者为什么它只发生在 (int) 而不是其他更复杂的类型上:
HashMap<?, ?> returncp = startAndWaitForJob("DSP_WF_" + operation, cp);
DSPResponseDTO dto = (DSPResponseDTO) returncp.get("RESPONSE_OBJECT");
String respDesc = (String) returncp.get("statusInfoResponseDescription");
int respCode = (int) returncp.get("statusInfoResponseCode");
编译错误:
[javac] int respCode = (int) returncp.get("statusInfoResponseCode");
[javac] ^
[javac] required: int
[javac] found: CAP#1
[javac] where CAP#1 is a fresh type-variable:
[javac] CAP#1 extends Object from capture of ?
已阅读的问题:
incompatible types and fresh type-variable
Bounded-wildcard related compiler error
Java: Wildcard Types Mismatch Results in Compilation Error
我不知道你的地图里有什么类型,但是这里
String respCode = (int) returncp.get("statusInfoResponseCode");
您将 get
调用的结果转换为 int
并将其分配给 String
。
如果是Integer
,使用
Integer respCode = (Integer) returncp.get("statusInfoResponseCode");
首先是什么CAP#1
我找到了这个 quote:
What on earth does "capture#337 of ?" mean? When the compiler encounters a variable with a wildcard in its type, such as the box parameter of rebox(), it knows that there must have been some T for which box is a Box. It does not know what type T represents, but it can create a placeholder for that type to refer to the type that T must be. That placeholder is called the capture of that particular wildcard. In this case, the compiler has assigned the name "capture#337 of ?" to the wildcard in the type of box. Each occurrence of a wildcard in each variable declaration gets a different capture, so in the generic declaration foo(Pair x, Pair y), the compiler would assign a different name to the capture of each of the four wildcards because there is no relationship between any of the unknown type parameters.
现在让我们回到消息CAP#1 extends Object from capture of ?
它认为 CAP#1
是 Object
的一个实例,更像是 class 扩展 class Object
.[=25 的一个实例=]
现在需要想象泛型在编译时被转换为强制转换。这意味着:
HashMap<?, ?> map = new HashMap();
int test = (int) map.get("");
大致相当于:
HashMap<?, ?> map = new HashMap();
int test = (int) (Object) map.get("");
不可能(或编译器不允许)将 Object
转换为 'int'。
当然,这个Object
是用来表示简单的
但是完全可以将 Object
转换为 'String' 或 'Integer'。
所以你可以做的是:
HashMap<?, Integer> returncp = startAndWaitForJob("DSP_WF_" + operation, cp);
在那种情况下,您甚至不需要投任何东西 (see)。
但如果由于任何原因无法做到:
int respCode = ((Integer) returncp.get("statusInfoResponseCode")).intValue();