不安全操作编译器错误是什么意思?

发布于 2024-08-20 02:05:15 字数 209 浏览 3 评论 0原文

这个错误是什么意思?

Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

有什么建议如何避免这种错误?

What does this error mean?

Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Any suggestions how to avoid that kind of error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揪着可爱 2024-08-27 02:05:15

这是当您使用 集合 而不指定类型。您可能会遇到这样的情况:

ArrayList myList = new ArrayList(); // or some other Collection class

如果是这种情况,您需要更改它以指定要存储的对象类型。例如:

ArrayList<String> myList = new ArrayList<String>();

阅读 Java 泛型了解更多信息。

这是我在没有看到您的代码和完整错误消息的情况下的最佳猜测。该消息可能还有其他原因,这正是我之前见过的伴随该消息的问题。

That's the error you get when you use Collections without specifying a type. You probably have something like:

ArrayList myList = new ArrayList(); // or some other Collection class

If that's the case, you need to change that to specify what type of objects you want to store. For example:

ArrayList<String> myList = new ArrayList<String>();

Read up on Java Generics for more information.

This is my best guess without seeing your code and the full error message. There could be other causes for that message, this is just the problem that I've seen accompany that message before.

书信已泛黄 2024-08-27 02:05:15

首先,使用 -Xlint:unchecked 重新编译以查看问题所在。然后解决这些问题。未经检查的警告有许多潜在的高级原因。一是您没有提供应该提供的类型参数。在某些情况下,它们是不可避免的,然后您可以抑制特定的警告,但这些都是例外,必须注意不要抑制真正重要的警告。

因此,如果您对所揭示的任何具体问题遇到困难,请使用 -Xlint:unchecked 重新编译并发布其他问题。

First, recompile with -Xlint:unchecked to see what the problem is. Then fix those problems. There are a number of potential high-level causes for unchecked warnings. One is that you didn't provide type parameters where you should have. There are some situations where they are unavoidable, and then you can suppress the specific warning, but these are the exception, and care must be taken that you aren't suppressing warnings that are really important.

So recompile with -Xlint:unchecked and post additional questions if you have trouble with any of the specific issues that are revealed.

天赋异禀 2024-08-27 02:05:15

这是什么意思?

Java泛型允许你写这样的东西:

List<String> l = ...;
String s = l.get(0);  // note there is no explicit typecast.

但是如果编译器告诉你你的代码有“未经检查或不安全的操作”,那就说明你已经违反了安全使用泛型的规则,你的代码可能 在意想不到的地方给出运行时类强制转换异常;例如,在上面的语句中我们省略了类型转换。

有一些事情会导致编译器抱怨未经检查或不安全的操作,每种情况都需要不同的补救措施。执行编译器告诉您的操作并使用 -Xlint 选项运行它。

What does it mean?

Java generics allow you to write something like this:

List<String> l = ...;
String s = l.get(0);  // note there is no explicit typecast.

But if the compiler tells you that your code has "unchecked or unsafe operations", it is saying that you have broken the rules for using generics safely, and your code may give runtime class cast exceptions at unexpected places; e.g. in the statement above where we left out the typecast.

There are a few things that will cause the compiler to complain about unchecked or unsafe operations, and each one requires a different remediation. Do what the compiler is telling you and run it with the -Xlint option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文