Will things run quicker if I make my variables final?
I'm writing for Android (Java).
I'm declaring int's and float's as part of an ongoing loop.
Some of them don't need to be changed after declaration.
If I set them all to final when declaring, will thing开发者_如何学JAVAs run quicker?
[Edit]
Thanks everyone. I didn't actually expect it to make any improvements, I just noticed, after browsing the source of various large projects, it was fairly common. Cheers
Things will not run quicker. The final
keyword is just compile time syntactic sugar.
If it were actually static final
, then you could take benefit of compiletime calculation and inlining of the value in any refernce. So, with for example:
private static final long ONE_WEEK_IN_MILLIS = 7 * 24 * 60 * 60 * 1000L;
public void foo(Date date) {
if (date.getTiem() > System.currentTimeMillis() + ONE_WEEK_IN_MILLIS) {
// No idea what to do here?
}
}
the compiler will optimize one and other so that it ends up like:
private static final long ONE_WEEK_IN_MILLIS = 604800000L;
public void foo(Date date) {
if (date.getTiem() > System.currentTimeMillis() + 604800000L) {
// No idea what to do here?
}
}
If you run a decompiler, you'll see it yourself.
Although setting to final might have impact on the speed, the answer will most probably be different for each VM or device.
Declaring them final, however, doesn't hurt, and one could even call it good programming style.
As for performance, this looks almost certainly like premature optimization. Profile, find bottlenecks, rethink your algorithms. Don't waste your time with "final" just because of performance - it will barely solve any problem.
If you also make it static (a class variable) it can increase performance, and it is also good programming practice to use final for variables that you know will not change. Though, you may not want it to be a class variable, in which case, I am not sure if can improves performance, but I think it may in many cases.
http://docs.sun.com/app/docs/doc/819-3681/6n5srlhjs?a=view
The dynamic compiler can perform some constant folding optimizations easily, when you declare constants as static final variables.
Declare method arguments final if they are not modified in the method. In general, declare all variables final if they are not modified after being initialized or set to some value.
So for example, if you have code that multiples two of your final variables, during run-time the VM may use what would normally be sleep/downtime to calculate the result of that multiplication so it doesn't have to do it in the busy periods.
I'd consider it a good practice to make variables final (you might use Eclipse's Preferences > Java > Code Style > Clean Up to do so). While performance might actually improve, I'd expect the differences to be negligible. In my opinion, it helps with readability of code though (i.e. no need to look for assignments) which certainly is a Good Thing (tm).
When we declare any variable final, means at compilation time it would be identified and while running the application JVM does not check it for any manipulation as it is declared as final(constant). so definately we are removind overhead from JVM.
so we could say it will improve performance, if depends on your case is the variable is constant make it final better if you make if static final.....
they are optimized by JVM are kept in the Constant Pool with the Classfile "http://negev.wordpress.com/java-memory-brief/"
精彩评论