且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么导致javac发出“使用未检查或不安全操作”警告

更新时间:2022-10-29 17:23:04

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();

use

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


In Java 7 you can shorten generic instantiation by using Type Inference.

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