It is common knowledge that widening and boxing is not allowed, but there must be some explanation for the following legal code:
I can't find provisions for widening followed by boxing in the JLS, but I must obviously be missing someting (either that, or the JLS needs to be revised, which I would find very surprising.)
I would appreciate some help with this.
Thanks,
Ruben
All code in my posts, unless a source is explicitly mentioned, is my own.
Correct me if I'm wrong ruben , but I don't see widening, I see a narrowing and then boxing:
Short <- short <- byte <- int
is my assumption correct?
scjp6-90%
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
0
armando fonseca wrote:Correct me if I'm wrong ruben , but I don't see widening, I see a narrowing and then boxing:
Short <- Object <- Byte <- byte <- int
is my assumption correct?
Armando,
The issue with your chain of conversions is that you can't have an implicit reference narrowing conversion (in your case that would be from Object to Short.) This is what is commonly called reference downcasting, and it requires an explicit use of the casting operator.
This is an interesting problem. Either I can't find something in the JLS, the JLS is missing something, or javac is not doing what it should. I'm leaning towards the first option, because otherwise my unshakable faith in the Java gods will be disturbed for ever (just kidding.)
The output of this gives
javap -classpath . -c Test
On both 1: and 6: above the bytecode looks identical
but I wish we could see more details in the bytecode, like primitive type conversions
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
0
That's very interesting, thanks Rashmi.
I'm mostly curious from the theoretical point of view (by theoretical I mean the official language specification,) because I do not think that anywhere in the JLS there is an explanation for what is taking place.
Rashmi Jaik
Ranch Hand
Joined: Oct 04, 2008
Posts: 50
posted
0
Yup, there doesn't seem to be a mention of the boxing conversion in 2 levels as discussed above
I'll try to see how this could be manually done prior to Boxing and Unboxing by compiling it with a previous version of Java
Rashmi Jaik
Ranch Hand
Joined: Oct 04, 2008
Posts: 50
posted
0
javac -source 1.4 -target 1.4 Test.java
Compiled this to pre-boxing version of Java
From #1 above , it looks like Short.valueOf() can take both short and byte primitive arguments, byte is widened to short primitive and then the valueOf , converts the short to Short.
But at #2 above, it fails to compile - because as you mentioned Byte and Short don't have any inheritance relationship between them.
This deduction is based on logic, and looking at the bytecode - not from the JLS
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
0
Thank you Rashmi. All this makes sense. I don't see anything unexpected there. But it doesn't explain why the type conversion is taking place. You can always use explicit casting operations, but the JLS is very explicit about what implicit type conversions are allowed, and byte to Short is not one of them.