Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final variable

 
zarina mohammad
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
zarina mohammad
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks corey for the explanation. that was really helpful
-zarina
 
Paulo Aquino
Ranch Hand
Posts: 202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Paulo Aquino
Ranch Hand
Posts: 202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
256-b=-56

Well, at a glance, this means that b = 256 + 56 = 312...
 
srinivas bolloju
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) ??
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic