One of my colleagues read this in a book of Data Structures, that in Java if a class is made as final(assuming that its not going to be inherited any further), then it increases the run time performance.
Can someone pls explain how does this happen? How is the bytecode of a final class is different from that of a normal class and in what way is its behaviour different at run time?
Well that's not really true making a class final is none but keeping it constant it's really got little to do with performence wich is highly dependent on the methods on the class so relax; best regard
hi this could be one of the reasons. inheritance involve key issue of polymorphism which is related to latebinding or dynamic binding.
if a class is final it cannot be inherited. its all methods are also final that means methods cannot be overridden. so no need of dynamic binding. only static binding and dynamic binding has more overheads than static binding. hence if a class or a method is final only static binding is involved (less overheads at runtime). thus improved performance is achieved.
With regards to the class file, the only difference is a flag set on the class (and probably each method as well) indicating that it is final.
With regards to performance, a jvm may also be optimized to inline final methods at run time when generating the machine code.
If you want to know more about how the jvm works, I suggest reading the Java Language and JVM specifications (URL's below). They are pretty well laid out and a relatively easy read.