aspose file tools*
The moose likes Beginning Java and the fly likes What is the role of Final here ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the role of Final here ?" Watch "What is the role of Final here ?" New topic
Author

What is the role of Final here ?

Parag Kale
Greenhorn

Joined: Apr 08, 2001
Posts: 25
Hi ! I had posted it on the Intermediate Forum but I am moving this here, as I think this forum is more apprpriate
Try this :
code:
--------------------------------------------------------------------------------
final int a = 100;byte b = a;System.out.println("The number is" + b) ;
--------------------------------------------------------------------------------

This code compiles and prints the output. But the following :
code:
--------------------------------------------------------------------------------
int a = 100;byte b = a;System.out.println("The number is" + b) ;
--------------------------------------------------------------------------------
Does not compile. It says "Possible loss of precision".
What is the role of final here ? Can anyone please explain this behaviour ?

rgds,
Parag
Lucas Richardson
Ranch Hand

Joined: Jul 08, 2001
Posts: 32
When A is declared final, it cannot be changed, and therefore the compiler has no problem storing the value 100 to a byte (B). When A is not declared final, the compiler recognizes the fact that the int value of A could be higher than what a byte could store, so it tells you about it. You have to explicitly cast it in order to tell the compiler that you are fully aware of what will happen (byte b = (byte) a
Then it will compile, however, if you do something like set a to 10000, you'll find that b = 16.
Laojar Chuger
Ranch Hand

Joined: Dec 20, 2000
Posts: 111
This is a very interesting feature! What happen if you put a static before final?
Lucas Richardson
Ranch Hand

Joined: Jul 08, 2001
Posts: 32
Try it.
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Well, it looks as though when you put final in front, that (for some reason unknown to me) the system does a cast for you. Notice that if you put 32000 into the value, the system will tell you that it can't convert. However if the value remains between -2^7 and 2^7-1 then the code will complile. Of course remember in your modified code below that you need to cast when you are going up the chain.
int a = 100;
byte b = a;
System.out.println("The number is" + b);
byte
short
int
long
float
double
This is your hierarchy. If you go down you are converting. If you go up, you must cast. So again for some reason when you put a final in from of the variable, it seems that the system will cast the value if it remains within the range of your upcasted type.
Hope this helps.
-Dale

------------------
What's this H2SO4 doing in my fridge?? ( thud )
[This message has been edited by Dale DeMott (edited July 10, 2001).]


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the role of Final here ?