Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 290
Roman Avatar asked Mar 10 '10 17:03

Roman


2 Answers

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>();
like image 91
Jacob Mattison Avatar answered Sep 20 '22 22:09

Jacob Mattison


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

like image 21
GuruKulki Avatar answered Sep 24 '22 22:09

GuruKulki