简体   繁体   English

通配符类型不匹配(int)编译错误,但不是(String)

[英]Wildcard Type Mismatch (int) compilation error, but not (String)

I've read several questions that are similar to mine, but I still don't undestand what's happening, or why it only happens for the (int) and not for the other more complex types: 我已经阅读了几个与我的问题类似的问题,但是我仍然不理解正在发生的事情,或者为什么它只对(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");

The compilation error: 编译错误:

[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 ?

The questions already perused: 已经仔细考虑的问题:

incompatible types and fresh type-variable 不兼容的类型和新鲜的类型变量

Bounded-wildcard related compiler error 有界通配符相关的编译器错误

Java: Wildcard Types Mismatch Results in Compilation Error Java:通配符类型不匹配导致编译错误

I do not know what types are in your map, but here 我不知道您的地图中有什么类型,但是在这里

String respCode = (int) returncp.get("statusInfoResponseCode");

you cast the result of the get call to an int and assign it to a String . 您将get调用的结果转换为int并将其分配给String

If it is an Integer , use 如果它是Integer ,请使用

Integer respCode = (Integer) returncp.get("statusInfoResponseCode");

First what is CAP#1 首先, CAP#1是什么

I found this quote : 我发现这句话

What on earth does "capture#337 of ?" “捕获的#337个”到底是什么? 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. 当编译器遇到类型为通配符的变量(例如rebox()的box参数)时,它知道必须有一些T,而box是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. 它不知道T代表什么类型,但是它可以为该类型创建一个占位符以引用T必须是的类型。 That placeholder is called the capture of that particular wildcard. 该占位符称为特定通配符的捕获。 In this case, the compiler has assigned the name "capture#337 of ?" 在这种情况下,编译器已分配名称“ 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. 每个变量声明中每出现一次通配符都会获得不同的捕获,因此在通用声明foo(Pair x,Pair y)中,编译器将为四个通配符中的每一个捕获都分配一个不同的名称。任何未知类型参数。

Now let's go back to the message CAP#1 extends Object from capture of ? 现在让我们回到消息CAP#1 extends Object from capture of ?

It considers the CAP#1 as an instance of Object , more like an instance of a class extending the class Object . 它将CAP#1视为Object的实例,更像是扩展Object的类的实例。

Now one need to imagine that the generic are transform into cast at compile time. 现在需要想象一下,泛型在编译时就转换为强制转换。 Which means that: 意思就是:

HashMap<?, ?> map = new HashMap();
int test = (int) map.get("");

is more or less the equivalent of: 大约等于:

HashMap<?, ?> map = new HashMap();
int test = (int) (Object) map.get("");

Which is not possible (or allowed by the compiler) to cast an Object into an 'int'. 不可能(或编译器允许)将Object转换为“ int”。 Of course, this Object is used to make it simple to represent. 当然,此Object用于简化表示。

But it is totally possible to cast an Object into a 'String' or an 'Integer'. 但是完全有可能将Object转换为“字符串”或“整数”。

So what you could do is: 因此,您可以做的是:

HashMap<?, Integer> returncp = startAndWaitForJob("DSP_WF_" + operation, cp);

In that case you will not even need to cast anything ( see ). 在这种情况下,您甚至不需要投射任何东西( 请参阅参考资料 )。

But if that is not possible for any reason: 但是,如果由于某种原因无法实现:

int respCode = ((Integer) returncp.get("statusInfoResponseCode")).intValue();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM