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

Strange behaviour of final keyword..Watch out

 
Satheesh Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out this example from "sandeep mock exam"
class MyClass
{
public static void main(String []args)
{
final int i = 100;
byte b = i;
System.out.println("i ="+b);
}
}
ans : o/p i =100
BUT
class MyClass
{
public static void main(String []args)
{
int i = 100;
byte b = i;......(1)
System.out.println(b);
}
}
I get a compiler error as Incompatible type declaration, so when I cast line (1) as byte b = (byte)i; it works fine with o/p 100
why is this strange behaviour with " final keyword"?, I am confused.
can somebody clear my doubt?
Thanks in advance
satheesh
 
George Toronto
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
plz check Rhalid book.
 
Tom Tang
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks really tricky. Can someone explain more?
 
Satheesh Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I reallly could not understand what is given in the book when I read it, could u pls explain to me in brief
Thanks in advance
 
Bob Moranski
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I think it has to do with compiling rules. When an int is declared final, its value is set forever at compile time. The compiler knows for sure that 100 is within the range of byte value. On the other hand, without declaring an int final, the compiler will have no way of knowing for certain that the int's value will be within byte's range. Therefore, without the final keyword, compiler requires the int to be cast to byte.
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must've seen this example at least three times in the last month. It's funny how some things just keep popping up.
 
Satheesh Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks bob for ur detail explanation
now I got it..,
satheesh
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tom Tang:
This looks really tricky. Can someone explain more?

hi Tom,
Tom java compiler goes for optimization and replaces the final variables with the values present in that.
Note that the final method call is faster than a non-final method call bcoz in case of final method it doesnt have to go for dynamic data lookup which is slow .
Cherry
 
Rat Ban
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hey...Satheesh Kumar,
Can u tell me where i can find this particuler Mock Exam??
************T H A N K S IN A D V A N C E ***************
round6
 
Varsha Dighe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is the one
http://www.tipsmart.com/javacert/ptp/basics.htm
Varsha
 
Rat Ban
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Varsha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic