• 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

cast

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the result?

it is compile and run with output 100.
can somebody give an explaination? (note:If i not declare final, then compile error.)
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is giving me deja vu - I've seen it elsewhere recently.
Normally this type of narrowing conversion will produce a compile error (unless explicitly cast like this: b = (byte)i ;) because most int values won't fit into a byte. The narrowing conversion is allowed if the compiler can determine that the conversion is safe. This is the case here because the value 100 will fit in a byte and the compiler knows at the assignment that i must equal 100 because it is final.
See what happens if you change the value to 200.
Does this help?
Tod

[This message has been edited by Tod Tryk (edited June 09, 2001).]
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try the above code without final modifier also
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The final modifier is doing some trick out here.
If u remove the final modifier from the variable declaration then the programme doesnt even compile (rightly because of narrowing). why the presence of final results in successful compilation. Why is final playing all such tricks.?
Does the JLS specify something to this effect?
Anand
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When u use the final modifier, it becomes a compile time constant. Since 100 is within the range of a byte,
it allows the assignment without an explicit cast.
If u remove the final modifier, the value of i cannot be guaranteed to fit into a byte, so the compiler will complain.
Also try compiling the code with i as final with value as 500.
Regards,
Sajida
 
This will take every ounce of my mental strength! All for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic