Search code examples
javaandroidperformancefinal

performance or optimization in Android : final local variable vs local variable


I doubt that final local variables are more optimized by the Java compiler or dex than the local variables without final. Is there any difference between final variable and variable as to the Android performance?

Thanks.

for-loop:

for (final int x : list) ret += x;

try-catch:

try { /* do something */ } catch (final IOException e) {}

function:

void echo(final String s) { print(s); }

Solution

  • I don't think there are any performance difference between declare final and local.We declare a variable final when we don't want someone override this variable that is you cannot change the value of final variable(It will be constant).This optimization applies only to primitive types and String constants, not arbitrary reference types. Still, it's good practice to declare constants static final whenever possible.