At my company a number of developers have encountered websites claiming that by applying the final modifier to local variables (primarily Objects) it actually helps improve the performance in terms of speed for a Java Web Application. This is strange to me because this is not the purpose of applying the final modifier to local variables. The final modifier should ONLY be used if a calling client has an opportunity to modify what object a specific object reference variable is referring to and this capability is not desired.
We are using WebSphere Portal 6.1, WebSphere Application Server 7.0, EJB 3, and JAX-RPC/JAX-WS web services for a portal.
Does anyone have any special insight into this matter?
Final objects are very expensive interms of performance. Because, if you declare something as final, it means that it will remain throughout the entire lifetime of the program.. Whereas, if you consider the normal non-final objects, if they do not have a purpose, they are taken care of the garbage collector (GC). But in the case of a final object, whether you use it or not, the GC won't consider it. This will result in performance loss due to memory management, as a large number of memory has to be managed because you are saying that you are going to declare local variables as final variables.
Even the Sun Microsystems' documentation reports that the usage of final objects should be highly cared about as they are directly linked to the program's performance.
Hope this information is usefull.
But in the case of a final object, whether you use it or not, the GC won't consider it.
Said object would be eligible for GC after the execution comes out of scope.
I have never heard of the 'final' keyword improving performance. I dont see any reason why it should.
If the claim were to be backed by a valid technical explanation, that would make me change my mind. But I doubt someone can provide one.
Deepak Bala wrote:I have never heard of the 'final' keyword improving performance. I dont see any reason why it should.
For a method it could, because it would mean there'd be no further method resolution. For a local, I'm still skeptical, but if the compiler knows the value will never be assigned it might be possible to do a micro-optimization of some sort.
If you have a local variable never assigned, you should delete that local variable anyway.
David Newton wrote: . . . For a local, I'm still skeptical, but if the compiler knows the value will never be assigned . . .
Here is the documentation that a Wipro contractor provided where there is what I now know are unsubstantiated claims that final local variables have a performance increase. Based on my personal review of this documentation, there is NO CONCLUSIVE evidence that final local variables are an optimization over non-final local variables. Here is the documentation where these performance claims can be made.
Check Style Rules:
I could still see method finality being a potential help, depending on how the JVM and any JITting work, but still not convinced about locals. Thanks for taking the time to pursue this further!