This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final variable" Watch "final variable" New topic
Author

final variable

zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
examine the following code:

why is there no compiler error of loss of precision only when variable i is declared as final variable.??
am i missing something very important here?
clear me on this please.
thanks
zarina
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
That line of code works for the same reason this line works:

The compiler knows what value is going to be assigned to the variable and can verify that the value will fit into that variable. Therefore, the error is suppressed.
If you were to try to assign an int variable, like this, to a byte, you'd get a compiler error:

The compiler has no way of knowing that the value stored in i will be within the range of a byte.
However, if we make the variable i final, then we're really back to the first case. The compiler knows what value will be stored in i and can verify that the value will fit within the byte b.
From the JLS, §4.5.4 final Variables:

Once a final variable has been assigned, it always contains the same value.

You can also look at this section 5.2 Assignment Conversion for more information.
I hope that helps,
Corey


SCJP Tipline, etc.
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
thanks corey for the explanation. that was really helpful
-zarina
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
i was a little confused here...

Corey McGlone says:
If you were to try to assign an int variable, like this, to a byte, you'd get a compiler error:

code:
--------------------------------------------------------------------------------
int i = 100;byte b = i;
--------------------------------------------------------------------------------
The compiler has no way of knowing that the value stored in i will be within the range of a byte.

i know this code produces a "Loss of precision" compile error but my question is...
the variable i is already initialized to 100, so how come the compiler still doesn't know the value that it needs to assign to byte b.


Be Afraid...Be very Afraid...
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
As far as the compiler is concerned, if i is not declared as final, it's value can change at any point in the programme. when it's declared as final, then the compiler is happy.
suppose you had
int i = 100;
i += 200; <--- this line is allowed by compiler so value of i can change
byte b = i;
but if you had
final int i = 100;
i+= 200; <--- compiler error as final var can't be modified
byte b = i;


Amir
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
Mr. AMir,
I tried your code

but it still produces the compile error: "Possible Loss of precision"
Anyone out there kind enough to explain this things to me? Mr. amir thanks for the reply...
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
If int is not declared final and u wish to assign it to char it needs cast as the value can be changed anytime and then can go out of range.
like
int i=100;
i=i+100;
byte b=i;
will give compile error
but if int is declared final and within range of byte then u can asssign without cast like
final int i=100;
byte b=i;
will compile fine
but if int is final and out of range then needs cast
final int i=200;
byte b=(byte)i;
value in b will be 256-b=-56;
there is loss of precesion but no error
Thats all i know
HTH
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
Mr Paulo,
The code that i put in there was not supposed to compile but to illustrate the behaviour of declaring variable i as non-final and final.
the whole point is that once a variable is not declared as final, then it's value can change and the compiler has no way of knowing the absolute value of the variable, in this case i, before it can store it in a byte variable. but if i is declared as final, then the compiler is certain that the value of i will not change while the programme is executing
HTH
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Originally posted by Deepali Pate:
but if int is final and out of range then needs cast
final int i=200;
byte b=(byte)i;
value in b will be 256-b=-56;

Where did you get this formula? I find it easier converting i to binary, then getting only the 8 least significant bits, which in this case, translates to -56.
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
I dont quite remember but i guess some mock. Why is it wrong formula. Tillnow it has given right ans. I hate converting to bits so i find this simpler.
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
Mr. Amir,
Thanks for the reply! Now i got your point! This will help in my SCJP review, oce again thanks!
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
256-b=-56

Well, at a glance, this means that b = 256 + 56 = 312...
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
hi,

i dont see any difference in these below 2 except final
1)
int i=200;
byte b =(byte) i;
2)
final i=200;
byte b =(byte) i;
both will give compiler error if not casted. using final doesnt give loss of precision if value is within the byte range. if the value goes out of byte range then variable with or without final is same(without cast) ??


please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
in this case it will cause compiler error because the byte range is -128 to +127.
it cannot fit 200 to a byte variable, be it final or not!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final variable
 
Similar Threads
Compile time constant ?
implicit and explicit casting
Significance of 'final' modifier
switch statement
Legal arguments in switch case statements