简体   繁体   English

无与伦比的类型:java.lang.Class<capture#1 of ? extends T[]> 和 java.lang.Class<java.lang.Object[]>

[英]Incomparable types: java.lang.Class<capture#1 of ? extends T[]> and java.lang.Class<java.lang.Object[]>

I was trying to understand casting in java while I was playing with a few methods inside the Arrays Class.当我在 Arrays 类中使用一些方法时,我试图理解 java 中的转换。

I am using IntelliJ IDEA and in the following method我正在使用 IntelliJ IDEA 并采用以下方法

public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    @SuppressWarnings("unchecked")
    T[] copy = ((Object)newType == (Object)Object[].class)
        ? (T[]) new Object[newLength]
        : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;
}

in the following line:在以下行中:

T[] copy = ((Object)newType == (Object)Object[].class)

IDE suggests that casting to (Object) is redundant. IDE 建议强制转换为 (Object) 是多余的。

Removing the casting is causing the following error.移除铸件会导致以下错误。

Incomparable types: java.lang.Class<capture#1 of ? extends T[]> and java.lang.Class<java.lang.Object[]>

which made me thing that at first the error is correct, but experimenting for a while made me realise that I did not understand the issue because refactoring the code like ->这让我觉得一开始这个错误是正确的,但是经过一段时间的试验让我意识到我不理解这个问题,因为重构代码像 ->

T[] copy = (newType == (Object)Object[].class)

and

T[] copy = ((Object)newType == Object[].class)

does NOT produce this error.不会产生这个错误。

I cannot figure out why it does not produce this error, could someone explain?我无法弄清楚为什么它不会产生此错误,有人可以解释一下吗?

The incomparable types error is telling you that it doesn't make sense to compare two things that cannot possibly be equal.不可比类型错误告诉您,比较不可能相等的两个事物是没有意义的。

For example, there's no point in Integer.valueOf(0) == "" , because they're not the same types;例如,在Integer.valueOf(0) == ""没有意义,因为它们不是相同的类型; nor is one a supertype of the other.一个也不是另一个的超类型。 It will always be false.它永远是假的。

The compiler will prevent the a == b if both are class types (as opposed to interfaces), and both a = b and b = a would be disallowed.如果a == b都是类类型(而不是接口),则编译器将阻止它们,并且a = bb = a都将被禁止。

So, you are being told that a Class<? extends T[]>所以,你被告知Class<? extends T[]> Class<? extends T[]> cannot be equal to a Class<Object[]> , because you can't assign a reference of one type to a variable of the other type. Class<? extends T[]>不能等于Class<Object[]> ,因为您不能将一种类型的引用分配给另一种类型的变量。

By casting one of the references to Object , the compiler no longer knows (/thinks) that the types are definitely not related - because Object is a supertype of everything, so "nor is one a supertype of the other" is no longer true - so the compiler allows the check.通过将一个引用强制转换为Object ,编译器不再知道(/认为)这些类型绝对不相关 - 因为Object是一切的超类型,所以“一个也不是另一个的超类型”不再是真的 -所以编译器允许检查。


One thing that is redundant in that method is U .有一件事情多余的那种方法U There's no need for it, just use Object[] in the parameter type instead.不需要它,只需在参数类型中使用Object[]

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

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