Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in assignment

 
saran bala
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test9{
public static void main(String a[]){
final byte b = 1;
char c = 2;
short s = 3;
c = b; // 1
}
}
In this code,if byte is declared as final,while assigning byte to char,there is no problem in compilation.But if byte is not final,then compilation fails at lime marked 1.Why is it so?
 
Doug Slattery
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saran,

Originally posted by saran bala:

In this code,if byte is declared as final,while assigning byte to char,there is no problem in compilation.But if byte is not final,then compilation fails at lime marked 1.Why is it so?


By declaring a variable final means it gets special treatment at compile time. To add to the confusion, it also gets treated differently as a local variable as opposed to an instance variable.

Try fiddling around with the following snippet to see how it works:



Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic