Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

narrowing primitive conversion

 
sriram sandhya
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was trying to compile a statement.
int i = 20;
final int j = 20;
byte a = i; //Fails
byte b = j; //does not complain.
In the book I am reading the author says that since the final value of j is in the range of byte b, it won't complain. then why does the line before fails. Is it because we do not know the value of i for sure(unlike j) and it might go out of range of byte a?
Any help will be greatly appreciated
Sriramsandhya
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are understanding the situation. The compiler can determine (and link) the value of the final variable at compile time. The other variable is not final, so the "link" cannot be made (and the value guaranteed) at compile time and so the compiler error.
 
sriram sandhya
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic