java - Compilation effects of declaring method parameters "final" -
this question has answer here:
marking method parameter final
needed allow variable access inner anonymous classes , useful coding-tool enforcing variable use conventions within method.
apart that, there other compilation-related motivation use final
-keyword? specifically, give compiler hints facilitate specific optimization otherwise not performed? or compiler smart enough recognize if variable never modified anyways?
for example, i'm thinking whether there difference how variables passed method. e.g. variable of primitive type (int, byte, etc.) needs copied when passed method prevent modifications trickle out calling code, whereas if parameter marked final
, "passed reference" without such concerns, i.e. no copy-operation needed.
but let's keep question more general example:
are there scenarios adding final
method parameter change generated byte-code?
no there not. purely compile time check.
note bytecode higher level seem think - in particular there's no raw memory addresses. parameter passing abstracted away. also, java compiler little optimization. jit responsible doing optimization @ runtime.
furthermore @ actual machine code level, passing single word parameter reference isn't going optimize anything. under normal circumstances, copying faster (since it's in register anyway). again, low level implementation details jit worries about, not purview of bytecode.
Comments
Post a Comment