In java bytecode why is the receiver is pushed onto the stack first followed by all the parameters? I seem to remember it being something to do with efficiency.
This is true for both method calls and setting fields.
Method Call
class X {
int p(int a) {
//Do something
}
int main() {
int ret = p(1);
}
}
Main method compiles to:
aload_0 // Load this onto the stack
iconst_1 // Load constant 1 onto the stack
invokevirtual <int p(int)> from class X
Setting a field:
class X {
int x;
int main() {
x = 1;
}
}
Main method compiles to:
aload_0 // Load this onto the stack
iconst_1 // Load constant 1 onto the stack
putfield <int x> from class X
Being pushed first has advantages in that
Are you asking why it is pushed at all? In both cases you're accessing something that belongs to an instance of the class, so this
has to be part of that process.
Are you asking why it is pushed first? Just Java convention. I guess it's convenient to always have this
first regardless of the many things that could follow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With