【转】http://121dog198.blog.163.com/blog/static/50859950201422612247819/
1. V
Some VMs (like HotSpot) may do things more intelligently and know when methods are/are not overridden and generate faster code as appropriate.
虚函数一般是通过函数表实现的,这个虚函数表其实是一个函数指针,每次函数调用都需要查询函数指针,如果一个类被标为final,那么所有的方法不会被重载,也就不需要这样的函数指针了,因而性能变得很好。 现在很多虚拟机都非常智能化,以至于能够判断方法是否被重载,从而产生更快的codes。
2.The best use of final that I've seen is in library code: Making a class and/or methods final, in a library class, prevents your customers from extending it, making their code overly dependent on your implementation. Doing this enables you to make changes to your library class with confidence that they won't break client code. I'm often angry when vendors do this to me, the customer, because it prevents mocking and other good things. But I see why they do it.
可以阻止你的客户集成你实现的类,当你修改自己的库时,不用担心打牌客户的codes。
3.
3.1. Final keyword improves performance. Not just JVM can cache final variable but also application can cache frequently use final variables.
jvm能够缓存final变量,应用程序也能够缓存频繁使用的final变量3.2. Final variables are safe to share in multi-threading environment without additional synchronization overhead.
final变量能够在多线程环境下安全使用
3.3. Final keyword allows JVM to optimized method, variable or class.