What is unchecked and unsafe operation here?

南笙酒味 提交于 2019-12-10 13:50:16

问题


I have the following code:

private static final Set<String> allowedParameters;
static {
    Set<String> tmpSet = new HashSet();
    tmpSet.add("aaa");
    allowedParameters = Collections.unmodifiableSet(tmpSet);
}

And it cause:

Note: mygame/Game.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

And when I recompile with the suggested option I see a pointer (^) pointing at "new" in front of HashSet();.

Does anybody know what is going on here?


回答1:


Yes, you're creating a new HashSet without stating what class it should contain, and then asserting that it contains strings. Change it to

 Set<String> tmpSet = new HashSet<String>();



回答2:


these messages occur when you are using classes that support the new J2SE 1.5 feature - generics. You get them when you do not explicitly specify the type of the collection's content.

For example:

List l = new ArrayList();
list.add("String");
list.add(55);

If you want to have a collection of a single data type you can get rid of the messages by:

List<String> l = new ArrayList<String>();
list.add("String");

If you need to put multiple data types into once collection, you do:

List<Object> l = new ArrayList<Object>();
list.add("String");
list.add(55);

If you add the -Xlint:unchecked parameter to the compiler, you get the specific details about the problem.

for more details refer here : http://forums.sun.com/thread.jspa?threadID=584311



来源:https://stackoverflow.com/questions/2419216/what-is-unchecked-and-unsafe-operation-here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!