Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"uses unchecked or unsafe operations" [duplicate]

Tags:

Why am I getting "uses unchecked or unsafe operations" error everytime i compile? What's wrong with the code? I copied the exact same code from this tutorial http://www.mkyong.com/java/json-simple-example-read-and-write-json/

import java.io.FileWriter;
import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONObject;

public class JsonSimpleExample {
    public static void main(String[] args) {

        JSONObject obj = new JSONObject();
        obj.put("name", "mkyong.com");
        obj.put("age", new Integer(100));

        JSONArray list = new JSONArray();
        list.add("msg 1");
        list.add("msg 2");
        list.add("msg 3");

        obj.put("messages", list);

        try {
            FileWriter file = new FileWriter("c:\\test.json");
            file.write(obj.toJSONString());
            file.flush();
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.print(obj);
    }
}
like image 663
user3589718 Avatar asked May 20 '14 01:05

user3589718


People also ask

How do you fix unchecked or unsafe operations?

How to resolve warning message: uses unchecked or unsafe operations. You can resolve this warning message by using generics with Collections. In our example, we should use ArrayList<String> rather than ArrayList() . When you will compile above code, you won't get warning message anymore.


2 Answers

The uses unsafe or unchecked operations warning is displayed when you execute code which the Java compiler considers to be lacking in error-checking, or potentially unsafe in some way. However, it's a warning, not an error, and will not stop your code from compiling -- large projects will often churn out warning after warning, and you're free to determine whether they're worth taking action on or not. If you want to dig deeper into what's causing the warning to trigger, you can recompile your .java file with the syntax javac -Xlint:unchecked yourfilename.java, and the compiler will give you more verbose information as to what exactly is causing the error.

In my experience, this warning can often be caused by using something like an ArrayList without specifying the type which it should expect to hold (i.e. using ArrayList a = new ArrayList() rather than ArrayList<String> a = new ArrayList<String>()). The compiler is, in my example case, warning you that your code isn't going to do any checking for you that the values you add to it are any particular type. In a production application, it would likely be good to specify types, but in a test app, you're free to ignore the warnings if you're not concerned about them.

like image 158
Sam Hanley Avatar answered Sep 20 '22 21:09

Sam Hanley


You get an unchecked cast usually when you cast a generic class, for example:

// Here we have unchecked cast warning
ArrayList<String> arr = (ArrayList<String>) obj;// obj is of type Object

one way to prevent this and make the cast safe is to extend the type which is cast and then use your custom type which extends that like this:

// your class extends generic but is not generic
class MyClass extends ArrayList<String> {  }

//then change your cast like this: 
MyClass arr = (MyClass) obj;//here we have NO warning for unchecked cast
like image 44
ygngy Avatar answered Sep 20 '22 21:09

ygngy