I am reading about JVM architecture. Today I read about the concept of the Operand Stack. According to an article:
The operand stack is used during the ex
But could's not understand exactly that what it is and how it works in jvm?
The JVM defines virtual computer, and the instruction set of that computer is stack based. What this means is that instructions in the JVM instruction set will typically push and pop operands from the stack. So for example,
@T.J.Crowder's answer gives a more concrete example in lots of detail.
How the operand stack is implemented is platform specific, and it depends on whether code is being interpreted or whether it has been JIT compiled.
In the interpreted case, the operand stack is probably an array that is managed by the interpreter code. The push and pop micro-operations would be implemented something like:
stack[top++] = value;
and
value = stack[--top];
When the code is JIT compiled, the bytecode instruction sequences are transformed into native instruction sequences that achieve the same thing as the bytecodes did. The operand stack locations get mapped to either native registers or memory locations; e.g. in the current native stack frame. The mapping involves various optimizations that are aimed at using registers (fast) in preference to memory (slower).
Thus in the JIT compiled case, the operand stack no longer has a clear physical existence, but the overall behaviour of the compiled program is the same as if the operand stack did exist1.
1 - Actually, it may not be exactly the same when you take the Java memory model into account. However, the memory model places clear boundary on what the differences may be. And in the case of a single threaded computation that doesn't interact with the outside (e.g. I/O, clocks, etc), there can be no observable differences.