简体   繁体   中英

Java Static/Instance method optimization

In Java are unmodified method variables, that lack the final , qualifier re-initialized each time in a

  1. static method
  2. instance method

If the answer to 1. or 2., (or both) would the final qualifier allow Java to perform an optimization and store the method variable only once?

If the answer depends on the type of the variable, which type of variables are optimized/unoptimized? For instance, is String , int optimized while Map not optimized?

For comparison, Java would only store a static class variable such as

private static final String foo = "Teenage Mutant Ninja Turtle";

once. To clarify: the question is whether or not

1:

static SomeReturnValueOrVoid SomeMethod() {
    // 1.a Not modified, is this reinitialized each method call?
    String foo = "Teenage Mutant Ninja Turtle";

    // 1.b Marked final, is this reinitialized each method call?
    final String bar = "Teenage Mutant Hero Turtle"; 
}

2:

SomeReturnValueOrVoid SomeMethod() { // not static
    // 2.a Not modified, is this reinitialized each method call?
    String foo = "Teenage Mutant Ninja Turtle";

    // 2.b Marked final, is this reinitialized each method call?
    final String bar = "Teenage Mutant Hero Turtle"; 
}

is equivalent to

3:

class SomeClass {
    static final String foo = "Teenage Mutant Ninja Turtle";

    SomeReturnValueOrVoid SomeMethod() {
        // Uses foo
    }

    static SomeReturnValueOrVoid SomeMethod() {
        // Uses foo
    }

    ...
}

Be it a static method or final variable or both, makes no difference. The scope of a local variable means that this assignment will happen every time (except for when it's optimised*).

However, when it comes to the strings in your example, the strings will come from the string pool . So the assignments happen each time, but they will all reference the same string instance.

*Optimisation - The JIT compiler may inline the values, so each solution may run exactly as fast as another. So you should not try to micro optimize yourself unless you are aware of an actual performance problem.

In Java are unmodified method variables, that lack the final, qualifier re-initialized each time

(that the method is called) - of course they are, otherwise they would be uninitialized.

I think what you really wanted to ask was, can this initialisation be optimized out in some way? If the variable is assigned a constant value, then yes; the JIT compiler might remove the variable altogether, and replace references to it with its (known constant) value.

would the final qualifier allow Java to perform an optimization and store the method variable only once?

No. A final -qualified local variable can not have its value changed, but each invocation of a method has its own copy of the variable, and different instantiations might use different values for the same final variable. Consider:

void someMethod()
{
   final int number = new Random().nextInt();
}

The value cannot be assigned only once during program execution, because it is different each time the method is called.

The final modifier on a local variable has little or no direct effect on optimisation. It only places restrictions on what can be done with the variable - restrictions which might be followed for any variable, regardless of final qualification - and this in turn allows certain optimisations. If the optimizer is worth its salt, it won't care if the variable is declared final and will instead consider if it is effectively constant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM