Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use of final and output of this question?

 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
i have the following code
byte a;
final int b=20;
a=b;
System.out.println(a);

here we are assigning an integer directly to the byte variable .but it is not creating any errors .but if i remove that final then it shows.i want to know what effect final keyword makes here
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here we are assigning an integer directly to the byte variable


You can as long as it fits in to the size of the byte (-128 to 127). By making variable b final (i.e: you can't modify the value once assigned), compiler can make sure whether the value can fit in to a byte. Hope you got it
[ October 10, 2008: Message edited by: Vijitha Kumara ]
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the documentation on the final keyword? One random link is here

Also are you saying that if you remove the final it causes an error?
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic