• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Type conversion not specified by JLS

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if looking at the bytecode helps

here's your code:



Compiled to create .class file

javac Test.java

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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, there doesn't seem to be a mention of the boxing conversion in 2 levels as discussed above

All I see is this:

http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#25214
byte to short, int, long, float, or double , so I guess byte primitive is directly converted to short primitive and then
public static Short valueOf(short s) , is called to convert the short primitive to Short object

this is what armando explained 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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic