gpt4 book ai didi

java - 对象到泛型类型转换的未检查警告的定义在哪里?

转载 作者:行者123 更新时间:2023-12-02 10:11:23 24 4
gpt4 key购买 nike

JLS 5.1.9定义未经检查的转换如下:

Let G name a generic type declaration with n type parameters.

There is an unchecked conversion from the raw class or interface type G to any parameterized type of the form G<T1,...,Tn>.

There is an unchecked conversion from the raw array type G[] to any array type type of the form G<T1,...,Tn>[].

Use of an unchecked conversion causes a compile-time unchecked warning unless G<...> is a parameterized type in which all type arguments are unbounded wildcards, or the unchecked warning is suppressed by the SuppressWarnings annotation

据我所知,它表示如果将原始类型强制转换为具有有界类型参数的泛型类型,则会发生未经检查的转换。那么为什么以下代码会生成未经检查的警告:

public class DoubleStar{

public static void print(Object object){
((A<String>)object).print();
}

public static void main(String[] args){
print(new Object());
}
}

class A<T>{
public void print(){
System.out.print("HELLO");
}
}

由于对象不是原始类型(AFAIK)为什么上面的代码会生成未经检查的强制转换警告以及此行为是在哪里定义的?

最佳答案

您正在查看错误的部分,这就是为什么它在您的示例上下文中似乎没有意义。

您收到的警告是未经检查的强制转换。您正在查看有关未检查转化的部分 ( related )。

See 5.5.2 "Checked Casts and Unchecked Casts"

A cast from a type S to a type T is statically known to be correct if and only if S <: T (§4.10).

A cast from a type S to a parameterized type (§4.5) T is unchecked unless at least one of the following conditions holds:

  • S <: T
  • All of the type arguments (§4.5.1) of T are unbounded wildcards
  • T <: S and S has no subtype X other than T where the type arguments of X are not contained in the type arguments of T.

A cast from a type S to a type variable T is unchecked unless S <: T.

An unchecked cast from S to T is completely unchecked if the cast from |S| to |T| is statically known to be correct. Otherwise, it is partially unchecked.

An unchecked cast causes a compile-time unchecked warning, unless suppressed by the SuppressWarnings annotation (§9.6.3.5).

A cast is checked if it is not statically known to be correct and it is not unchecked.

If a cast to a reference type is not a compile-time error, there are several cases:

  • The cast is statically known to be correct.

No run-time action is performed for such a cast.

  • The cast is a completely unchecked cast.

No run-time action is performed for such a cast.

  • The cast is a partially unchecked cast.

Such a cast requires a run-time validity check. The check is performed as if the cast had been a checked cast between |S| and |T|, as described below.

  • The cast is a checked cast.

Such a cast requires a run-time validity check. If the value at run time is null, then the cast is allowed. Otherwise, let R be the class of the object referred to by the run-time reference value, and let T be the erasure (§4.6) of the type named in the cast operator. A cast conversion must check, at run time, that the class R is assignment compatible with the type T, via the algorithm in §5.5.3.

Note that R cannot be an interface when these rules are first applied for any given cast, but R may be an interface if the rules are applied recursively because the run-time reference value may refer to an array whose element type is an interface type.

关于java - 对象到泛型类型转换的未检查警告的定义在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54979291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com