“使用未经检查或不安全的操作” [英] "uses unchecked or unsafe operations"

查看:177
本文介绍了“使用未经检查或不安全的操作”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么每次编译时都会出现使用未经检查或不安全的操作错误?代码有什么问题?我复制了本教程中完全相同的代码 http: //www.mkyong.com/java/json-simple-example-read-and-write-json/

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


推荐答案

使用不安全或未检查的操作在执行Java编译器认为缺少错误检查的代码时会显示警告,或者在某种程度上可能不安全。但是,这是一个警告,而不是错误,并且不会阻止您的代码编译 - 大型项目通常会在警告后生成警告,您可以自由决定是否值得采取行动。如果您想深入了解导致警告触发的内容,可以使用语法 javac -Xlint重新编译 .java 文件:取消选中yourfilename。 java ,编译器会给你更详细的信息,说明导致错误的原因。

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.

根据我的经验,这个警告经常会使用类似ArrayList的东西而不指定它应该保持的类型(即使用 ArrayList a = new ArrayList()而不是 ArrayList< ; String> a = new ArrayList< String>())。在我的示例中,编译器警告您,您的代码不会检查您添加到它的值是否为任何特定类型。在生产应用程序中,指定类型可能会很好,但在测试应用程序中,如果您不关心它们,则可以自由地忽略警告。

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.

这篇关于“使用未经检查或不安全的操作”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆