• Post Reply Bookmark Topic Watch Topic
  • New Topic

strange casting and conversions  RSS feed

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ck out the follwing code:

my understanding says it should give a compile time error possible loss of precision.
But it compiles
And if we remove the final from i the as per my assumption it gives the error.
Question is what is this final doing here to compile the code neatly in first case.
Pinky
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by definition -- when you have final int i = 10 -- the compiler knows without a doubt, that the value of i is going to be 10. All the time.
So.... the compiler is also smart enough to know that the value 10 can fit into a byte without any loss of precision.
BUT -- when you take away that final modifier -- the comipler can't guarantee what the value of the variable 'i' will be. So it throws a compile time error saying so and YOU have to take the responsibility (by casting it) to say -- yes I understand that there might be a loss of precision -- but I want you to do it anyway.
ok.... so given that -- why does this give a "loss of precision" compile time error:
 
pinky yadav
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks that was a clear explaination.
but does this mean if i leave the final var unassigned and later assign value using some variable cast will be required ?
Pinky
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

OK Sant why won't this compile?
should the compiler know that in this case i is going to be 30. If I is assigned at compile time then it should know the value of k and l. Even if k and l are not assigned a value, they get the default value of 0.
Could you please explain why this fails to compile?
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ryan,
The compiler doesn't know that i is going to be 30. Even though i is final, k and l are not and can be changed. Hence i has to be evaluated at runtime.
If you were to make k and l final, it would compile ok.
 
Ryan Wilson
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ravi
Can you give me an example in this case where k + l could be changed before i is intialized?
 
Ryan Wilson
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ravi
Can you give me an example in this case where k + l could be changed before i is intialized?
 
Ravi Anamalay
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan,
If say a static block were called before i was initialized :
class FinalInt
{

static byte j;
static int k = 10;
static int l = 20;

static
{
k++;
}

static final int i = k + l;
public static void main(String[] args)
{
j = (byte)i;
}

}
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!