简体   繁体   中英

What is unchecked and unsafe operation here?

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM