Java “使用未经检查或不安全的操作”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23749786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:59:30  来源:igfitidea点击:

"uses unchecked or unsafe operations"

java

提问by user3589718

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/

为什么每次编译时都会出现“使用未经检查或不安全的操作”错误?代码有什么问题?我从本教程中复制了完全相同的代码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);
    }
}

回答by Sam Hanley

The uses unsafe or unchecked operationswarning 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 .javafile 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.

uses unsafe or unchecked operations当您执行Java编译器将考虑到缺乏的错误检查,或以某种方式可能不安全的代码显示警告。但是,这是一个警告,而不是错误,并且不会阻止您的代码编译——大型项目通常会发出一个又一个警告,您可以自由确定它们是否值得采取行动。如果您想深入了解导致警告触发的原因,您可以.java使用语法重新编译您的文件,javac -Xlint:unchecked yourfilename.java编译器将为您提供有关究竟是什么导致错误的详细信息。

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.

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

回答by Bahman

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

回答by Adnan Yousaf

I fix this problem by updating classpath version in project level gradle files

我通过更新项目级 gradle 文件中的类路径版本来解决这个问题

  classpath 'com.google.gms:google-services:4.2.0'