java IDE 报告的不必要的装箱检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25265383/
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-11-02 07:43:49  来源:igfitidea点击:

Unnecessary boxing inspection reported by IDE

javaide

提问by Juxhin

Unnecessary boxing inspection reported by IDE

IDE 报告的不必要的装箱检查

I was recently checking out some code that was posted on Oracle tutorials regarding Swing#JTable. There were a few warning messages that were returned by IntelIJ regarding the boxing of primitive variables inside the multidimensional array holding the Table data.

我最近查看了一些发布在 Oracle 教程上的关于 Swing#JTable 的代码。IntelIJ 返回了一些关于保存表数据的多维数组中原始变量装箱的警告消息。



Here is the array found which is taken from docs.oracle.com:

这是从docs.oracle.com 中找到的数组:

Object[][] data = {
            {"Kathy", "Smith",
                    "Snowboarding", new Integer(5), new Boolean(false)},
            {"John", "Doe",
                    "Rowing", new Integer(3), new Boolean(true)},
            {"Sue", "Black",
                    "Knitting", new Integer(2), new Boolean(false)},
            {"Jane", "White",
                    "Speed reading", new Integer(20), new Boolean(true)},
            {"Joe", "Brown",
                    "Pool", new Integer(10), new Boolean(false)}
    };


All the wrapped variables were receiving this message:

所有包装的变量都收​​到此消息:

"Unnecessary boxing 'new Integer(5)' Reports "boxing", e.g. wrapping of primitive values in objects. Boxing is unnecessary under Java 5 and newer, and can be safely removed. This inspection only reports if the project or module is configured to use a language level of 5.0 or higher."

“不必要的装箱 'new Integer(5)' 报告“装箱”,例如将原始值包装在对象中。装箱在 Java 5 和更新版本下是不必要的,可以安全地删除。此检查仅报告项目或模块是否配置为使用 5.0 或更高的语言级别。”



I know the concept of boxing and unboxing in Java, my question would be as to why it's 'irrelevant'in newer version of Java as I've seen many developers discuss it or use it recently.

我知道 Java 中装箱和拆箱的概念,我的问题是为什么它在较新版本的 Java 中“无关紧要”,因为我最近看到许多开发人员讨论或使用它。

Also, since boxing is not required what should 'new Integer(5)' be replaced with?

另外,由于不需要拳击,应该用什么替换“new Integer(5)”?

回答by Joni

Thanks to autoboxing in Java 5 and newer, you don't have to call the Integer and Boolean constructors to manually "box" the primitive values. The IDE seems to recommend you write the code as:

由于 Java 5 和更新版本中的自动装箱,您不必调用 Integer 和 Boolean 构造函数来手动“装箱”原始值。IDE 似乎建议您将代码编写为:

Object[][] data = {
            {"Kathy", "Smith", "Snowboarding", 5, false},
            {"John", "Doe", "Rowing", 3, true},
            {"Sue", "Black", "Knitting", 2, false},
            {"Jane", "White", "Speed reading", 20, true},
            {"Joe", "Brown", "Pool", 10, false}
    };