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

5

11

I have the following code in 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?

Amorphism answered 23/11, 2015 at 9:59 Comment(2)
I would do int iRef = s.Tripoli
That's not my question. My question is why does Integer iRef = s not work.Amorphism
S
18

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.

You can do

int is = s;    // widening

Short sRef = s;   // autoboxing

Integer iRef = (int) s;  // explicit widening, then autoboxing
Sweitzer answered 23/11, 2015 at 10:4 Comment(1)
You actually probably want int is = s & 0xffff; for widening, since it's common to use shorts in Java as unsigned 16-bit integers (there is no unsigned short type in Java other than char).Discant
K
2

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

Reference: Conversions and Promotions Reference

Kramatorsk answered 23/11, 2015 at 10:4 Comment(0)
I
2

Here´s the documentation from 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

Basicly the direct conversion from short to Integer is not part of the autoboxing process of 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.

Incarnation answered 23/11, 2015 at 10:4 Comment(0)
D
0

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.

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

It will run without producing any error.

Designedly answered 23/11, 2015 at 11:38 Comment(0)
D
0

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.

Discant answered 6/12, 2018 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.