是什么导致 javac 发出“使用未经检查或不安全的操作"?警告 [英] What causes javac to issue the "uses unchecked or unsafe operations" warning

查看:26
本文介绍了是什么导致 javac 发出“使用未经检查或不安全的操作"?警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

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

推荐答案

如果您使用没有类型说明符(例如,Arraylist() 而不是 Arraylist()代码>ArrayList()).这意味着编译器无法检查您是否以类型安全的方式使用集合,使用 泛型.

This comes up in Java 5 and later if you're using collections without type specifiers (e.g., Arraylist() instead of ArrayList<String>()). It means that the compiler can't check that you're using the collection in a type-safe way, using generics.

要消除警告,只需具体说明您在集合中存储的对象类型.所以,而不是

To get rid of the warning, just be specific about what type of objects you're storing in the collection. So, instead of

List myList = new ArrayList();

使用

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

<小时>

在 Java 7 中,您可以使用 类型推断.

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

这篇关于是什么导致 javac 发出“使用未经检查或不安全的操作"?警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆