Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

cast

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
try the above code without final modifier also
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!