This seems like it could be a common question but I searched SO and Google and couldn't find quite what I'm looking for:
What is the overhead of calls to the this
keyword in Java? I know in C++ there is some minimal overhead due to dereferencing the current object pointer. Does Java incur the same kind of overhead? Is it less optimal to make multiple calls to this
. It's mostly a question of readability vs. optimization.
None whatsoever. They both produce exactly the same bytecode. For example, this:
...produces:
...regardless of whether the line marked this line uses
a
orthis.a
. (Try it if you like - compile the above code both ways and compare both class files withjavap -c
.) Considering they produce exactly the same bytecode, there's no way there can be differences in performance.