java 为什么将 short 变量分配给 Integer 引用会产生编译时错误?

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

Why does the assignment of a short variable to an Integer reference produce a compile time error?

javacastingtype-conversionboxing

提问by kauray

I have the following code in Java :

我在 Java 中有以下代码:

class Boxing
    {
        public static void main(String args[])
        {
            short s = 10;
            Integer iRef = s;
        }
    }

Why does it produce an error in compilation? If I explicitly typecast the short to an integer in the expression, it compiles successfully. Since I'm using a short in an expression isn't the type of that supposed to be an integer by default without requiring the explicit case?

为什么会在编译时产生错误?如果我在表达式中显式地将 short 类型转换为整数,它将成功编译。由于我在表达式中使用 short 类型不是默认情况下应该是整数而不需要显式大小写的类型吗?

回答by Thilo

You want to have two things happening here: widening and auto-boxing.

你想在这里发生两件事:加宽和自动装箱。

Unfortunately, Java does only one of the two automatically. The reason for that is most likely that autoboxing was introduced fairly late (in Java5), and they had to be careful to not break existing code.

不幸的是,Java 只自动执行这两项中的一项。其原因很可能是自动装箱引入的很晚(在 Java5 中),并且他们必须小心不要破坏现有代码。

You can do

你可以做

int is = s;    // widening

Short sRef = s;   // autoboxing

Integer iRef = (int) s;  // explicit widening, then autoboxing

回答by rupesh_padhye

Boxing conversion converts expressions of primitive type to corresponding expressions of reference type. Specifically, the following nine conversions are called the boxing conversions:

装箱转换将原始类型的表达式转换为相应的引用类型的表达式。具体来说,以下九种转换称为装箱转换:

From type boolean to type Boolean

From type byte to type Byte

From type short to type Short

From type char to type Character

From type int to type Integer

From type long to type Long

From type float to type Float

From type double to type Double

From the null type to the null type

从类型 boolean 到类型 Boolean

从类型字节到类型字节

从短型到短型

从字符类型到字符类型

从类型 int 到类型 Integer

从类型 long 到类型 Long

从类型 float 到类型 Float

从双精度型到双精度型

从空类型到空类型

Reference: Conversions and Promotions Reference

参考:转换和促销参考

回答by SomeJavaGuy

Here′s the documentation from JLS 5.1.7

这是JLS 5.1.7的文档

Boxing conversion converts expressions of primitive type to corresponding expressions of reference type. Specifically, the following nine conversions are called the boxing conversions:

From type boolean to type Boolean

From type byte to type Byte

From type short to type Short

From type char to type Character

From type int to type Integer

From type long to type Long

From type float to type Float

From type double to type Double

From the null type to the null type

装箱转换将原始类型的表达式转换为相应的引用类型的表达式。具体来说,以下九种转换称为装箱转换:

从类型 boolean 到类型 Boolean

从类型字节到类型字节

从短型到短型

从字符类型到字符类型

从类型 int 到类型 Integer

从类型 long 到类型 Long

从类型 float 到类型 Float

从双精度型到双精度型

从空类型到空类型

Basicly the direct conversion from shortto Integeris not part of the autoboxing process of Java.

基本上,从shortto的直接转换Integer不是Java.

The autoboxing, as provided above, is only able to implicity cast the representing primitive type to it′s representing Wrapper class. Since this is not the case it will cause a compile time error.

如上所述,自动装箱只能将表示原始类型隐式转换为它的表示 Wrapper 类。由于情况并非如此,它将导致编译时错误。

回答by Rahul Vatsa

In the code considered.

在考虑的代码中。

class Boxing
{
    public static void main(String args[])
    {
        short s = 10;
        Integer iRef = s;
    }
}

Integer extends java.lang.Number. And java.lang.Short also extends java.lang.Number. But Short and Integer are not directly related if you wanted you can run the following program.

整数扩展 java.lang.Number。而 java.lang.Short 也扩展了 java.lang.Number。但是 Short 和 Integer 没有直接关系,如果您愿意,可以运行以下程序。

class Boxing
{
    public static void main(String args[])
    {
        short s = 10;
        Number iRef = s;
    }
}

It will run without producing any error.

它将运行而不会产生任何错误。

回答by Luke Hutchison

Java attempts to perform auto-widening, then auto-boxing, then auto-upcasting, but will not perform two of these for the same assignment. This is explained and diagrammed here, for the related case of method parameter assignment.

Java 尝试执行自动扩展,然后自动装箱,然后自动向上转换,但不会为同一个分配执行其中的两个。对于方法参数分配的相关案例,此处对此进行了解释和图示。