• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Narrowing + Boxing

 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to know what happens when the compiler interprets the declaration below:



I thought a int literal 200 couldn't be converted to a Short but only to Object or Integer.
I've tried to search on JVM spec but I couldn't find any kind of answer to this.

Reading the K&B, I understood that we can't Widen and Box, so why we can Narrow and Box?

Thanks!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adolfo Eloy wrote:
I thought a int literal 200 couldn't be converted to a Short but only to Object or Integer.
I've tried to search on JVM spec but I couldn't find any kind of answer to this.



See section 5.2 of the Java language specification. You want to go about 3 paragraphs down, to a paragraph that starts out with "in additon...". The second bullet point down talk about boxing and compile time constants.

Henry
 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Henry Wong!
I've searching on JVM spec instead of Java Language Spec. :P

Like you said, section 5.2 describes exactly what I was searching for.

Thank you!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic