• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Strange behaviour of final keyword..Watch out  RSS feed

 
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
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
plz check Rhalid book.
 
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
 
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.
 
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
 
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
 
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
 
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
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!