简体   繁体   中英

Widening and boxing with Java

In Java programming language widen and boxing doesn't work, but how does it work in following example?

final short myshort = 10;
Integer iRef5 = myshort; 

Why does this work? Is this not the same as widen and then box?

But if I write the following code:

final int myint = 10;
Long myLONG = myint;

why it doesn't work?

Following what others have said, I can confirm that I can compile your first example with the Eclipse compiler, but not the second. With the javac compiler, both don't compile, as stated by Vlad

This seems to be a bug in either compiler! Let's consult the JLS to find out, which one is right :-)

Neither works as it is (using javac 1.6.0_26 from Sun/Oracle, on Linux). See also here .

b.java:4: incompatible types
found   : short
required: java.lang.Integer
        Integer iRef5 = myshort; 
                        ^
b.java:7: incompatible types
found   : int
required: java.lang.Long
        Long myLONG = myint;
                      ^
2 errors

You can either widen or box, but you can't do both.

You can do

final int myint = 10;
Long myLONG = (long) myint;

With java 7 both the examples are not working. you will get below exception:

Type mismatch: cannot convert from short to Integer
Type mismatch: cannot convert from int to Long

Because the problem is not because of boxing but because of conversion.

Let's examine what you're trying to do in detail:

final short myshort = 10;
Integer iRef5 = myshort; 

The compiler will try to first box that short into an object, so that it can then perform the assignment (it cannot widen directly, since it is dealing with different types: an object and a primitive).

In short, this is equivalent to:

final short myshort = 10;
final Short box = new Short(myshort);  // boxing: so that objects are assignable.
Integer iRef5 = box;  // widening: this fails as Integer is not a superclass of Short

The same reasoning can be applied to your second example (which also fails), as is visible here . If your compiler does not complain on the first one, then there might be a bug with the compiler, because this is what's defined in the JLS. See the complete set of rules for conversion/promotion in the JLS here .

These are the promotion rules that apply to expressions in Java

all byte and short values are promoted to int

If one operand is long ,the whole expression is promoted to long

If one operand is a float ,the whole expression is promoted to float

If one operand is double ,the whole expression is promoted to double

Hence short value is promoted to int. This is not widening. "The conversion of a subtype to one of its supertypes is called widening"


You can box then wide but you can't widen and then box

So

final short myshort = 10;
Integer iRef5 = myshort; 

is equivalent to

final short myshort = 10;
Integer iRef5 = 10; 

which is perfectly valid

but

Long myLONG = 10;//this won't compile , 

But try with 10L it will so it will box then you can have it object too ie Object o = 10L;


See also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM