This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Final variables and downcasting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Final variables and downcasting" Watch "Final variables and downcasting" New topic
Author

Final variables and downcasting

del boy
Greenhorn

Joined: May 09, 2002
Posts: 6
Hello,
I do not understand why when we try to assign a FINAL variable of type primitive data type (eg. int ) to a lower type (eg. byte)
the compiler WILL NOT throw an error, only IF the value of the final variable is within range.... if you look at case 2 below if the variable
g is NOT final will have compiler error: POSSIBLE LOSS OF PRECISION, same with case 3 when final var is larger than data range of byte.
MY QUESTION IS: WHY with final variable in the first case we DO NOT have to explicitly downcast ???
Many thanks in advance!
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Because the compiler knows that the final variable can never be changed to something else so it will never be beyond the range.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Final variables and downcasting
 
Similar Threads
Simple Assignment Operator =
Legal Data Types
FINAL VARIABLE
Casting between int and byte
Primitive Assignments and Casting