Is it faster to access final local variables than class variables in Java?

前端 未结 5 1774
闹比i
闹比i 2020-12-06 01:07

I\'ve been looking at at some of the java primitive collections (trove, fastutil, hppc) and I\'ve noticed a pattern that class variables are sometimes declared as fina

5条回答
  •  情深已故
    2020-12-06 01:51

    it tells the runtime (jit) that in the context of that method call, those 3 values will never change, so the runtime does not need to continually load the values from the member variable. this may give a slight speed improvement.

    of course, as the jit gets smarter and can figure out these things on its own, these conventions become less useful.

    note, i didn't make it clear that the speedup is more from using a local variable than the final part.

提交回复
热议问题