什么是未经检查的演员阵容以及如何检查它?
我想我明白未经检查的演员意味着什么(从一种不同类型的演员到另一种类型的演员),但是“检查”演员意味着什么?如何检查强制转换以避免 Eclipse 中出现此警告?
I think I get what unchecked cast means (casting from one to another of a different type), but what does it mean to "Check" the cast? How can I check the cast so that I can avoid this warning in Eclipse?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
详细说明 Peter 所写的内容:
从非泛型类型到泛型类型的转换可能在运行时工作得很好,因为泛型参数在编译期间被删除,所以我们留下了合法的转换。但是,由于对类型参数的假设不正确,代码稍后可能会失败,并出现意外的
ClassCastException
。例如:第 3 行未经检查的警告表明编译器无法保证类型安全,即稍后可能会发生意外的 ClassCastException。事实上,这发生在第 4 行,它执行隐式转换。
To elaborate on what Peter wrote:
Casts from non-generic types to generic types may work just fine at runtime, because the generic parameters are erased during compilation, so we are left with a legitimate cast. However, the code may fail later with an unexpected
ClassCastException
due to an incorrect assumption regarding the type parameter. For example:The unchecked warning at line 3 indicates that the compiler is not able to guarantee type safety, in the sense that an unexpected ClassCastException may occur at a later point. Indeed, this happens at line 4, which performs an implicit cast.
未经检查的强制转换意味着您正在(隐式或显式)从泛型类型强制转换为非限定类型,或者反之亦然。例如这一行
会产生这样的警告。
通常出现此类警告是有充分理由的,因此您应该尝试改进代码而不是抑制警告。引自《Effective Java》第二版:
当然,消除警告并不总是像上面的代码那么容易。如果没有看到你的代码,就无法知道如何保证它的安全。
Unchecked cast means that you are (implicitly or explicitly) casting from a generic type to a nonqualified type or the other way around. E.g. this line
will produce such a warning.
Usually there is a good reason for such warnings, so you should try to improve your code instead of suppressing the warning. Quote from Effective Java, 2nd Edition:
Of course, it is not always as easy to eliminate warnings as with the code above. Without seeing your code, there is no way to tell how to make it safe though.
与检查的强制转换相反,未经检查的强制转换不会在运行时检查类型安全性。
以下是基于第三版的
考虑类型安全异构容器
部分的示例。 Joshua Bloch 的“Effective Java”,但容器类被故意破坏 - 它存储并返回错误的类型:如果
retrieve()
使用未经检查的强制转换 -(T)map.get(key)
- 运行此程序将导致ClassCastException
发生在Integer i = ints.get(0)
行。retrieve()
方法将完成,因为在运行时未检查实际类型:但如果
retrieve()
使用检查强制转换 -key.cast(map.get(key))
- 运行此程序将导致在key.cast(map.get(key))< 处发生
ClassCastException
/code> 行,因为检查后的强制转换会发现类型错误并抛出异常。retrieve()
方法将无法完成:看起来差别不大,但在未经检查的强制转换的情况下,
String
成功进入List< ;整数>
。在现实世界的应用中,这样做的后果可能......嗯,很严重。在检查演员表的情况下,尽早发现类型不匹配。为了避免未经检查的强制转换警告,如果程序员确实确定该方法实际上是安全的,则可以使用@SuppressWarnings("unchecked")。更好的选择是尽可能使用泛型和检查强制转换。
正如约书亚·布洛赫所说,
为了完整起见,这个答案涉及 Eclipse 的具体细节。
An unchecked cast, as opposed to checked cast, does not check type safety at runtime.
Here's an example based on the
Consider typesafe heterogenous containers
section of the 3rd ed. of "Effective Java" by Joshua Bloch, but the container class is intentionally broken - it stores and returns the wrong type:If the
retrieve()
uses an unchecked cast -(T)map.get(key)
- running this program will lead toClassCastException
occurring atInteger i = ints.get(0)
line. Theretrieve()
method will complete because the actual type was not checked at runtime:But if the
retrieve()
uses a checked cast -key.cast(map.get(key))
- running this program will lead toClassCastException
occurring atkey.cast(map.get(key))
line, because the checked cast will find out that the type is wrong and throw the exception. Theretrieve()
method will not complete:Little difference it may seem, but in the case with the unchecked cast, a
String
successfully made its way into aList<Integer>
. In real world applications, consequences of this may be... well, severe. In case with the checked cast, the type mismatch was discovered as early as possible.To avoid the unchecked casts warning,
@SuppressWarnings("unchecked")
can be used, if the programmer is really sure the method is in fact safe. The better alternative is to use generics and checked casts when possible.As Joshua Bloch put it,
For the sake of completeness, this answer deals with the Eclipse specifics.