• 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

Why Below Code Is Compile And Executes Successfully.

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

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the compiler can prove that the value of "num" will fit into "mm" without loss of precision.

If you remove the "final" qualifier, then it's a different situation.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By providing final, all we are doing is making it FINAL .. i mean the value can not be changed. Am I right?

In that case,

" // Why this statement is compiling? "
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I was trying to explain in the first sentence of my previous post; did that make sense to you?
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sbt Bhatta wrote:By providing final, all we are doing is making it FINAL .. i mean the value can not be changed. Am I right?

In that case,

" // Why this statement is compiling? "



Exactly. So by saying mm will always be 20 (or 40, or anything that can fit into an int and a byte) it will never be bigger than a byte. Since the compiler knows it will never have an issue with fitting mm into the byte num, it will just execute without throwing an exception.

EDIT: Fixed a typo.
 
Sbt Bhatta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aha ! thank you. Thank you. So in essence, the integer value can not be changed . By implicit casting it downwards, we are essentially changing the value. But the final means we cant change it. Cool
 
Sbt Bhatta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So in the above code if num is between -127 to 127, then the code compiles. If it is outside the range, then compiler errors with "loss of precision..."


However even if



we get a compiler error. Even though it is easy to see that 1 can easily fit in byte. So in essence to implicitly fit integer into byte one have to use final. Am I right? Please help.!!
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sbt Bhatta wrote:

So in the above code if num is between -127 to 127, then the code compiles. If it is outside the range, then compiler errors with "loss of precision..."


However even if



we get a compiler error. Even though it is easy to see that 1 can easily fit in byte. So in essence to implicitly fit integer into byte one have to use final. Am I right? Please help.!!



To get it to implicity cast to a byte from an int it has to be final. If you don't want it final, you could always put in an explicit cast, essentially telling the compiler "Hey, I know this is an int I'm putting in here, and it might be too big, but I know what I'm doing."



 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

we get a compiler error. Even though it is easy to see that 1 can easily fit in byte. So in essence to implicitly fit integer into byte one have to use final. Am I right? Please help.!!



The reason behind that is because of compile time constants versus dynamic variables. A final int's value cannot change and hence the compiler can guarantee that the value of that variable will or will not fit into an int. Try compile time constants of 127 128 129 -127 -128 -129 etc etc and you will see how it works
 
reply
    Bookmark Topic Watch Topic
  • New Topic