File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Below Code Is Compile And Executes Successfully.

 
V. Potluri
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Sbt Bhatta
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
W. Joe Smith
Ranch Hand
Posts: 710
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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."



 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic