I was going through the stack frame, so every function call gets pushed into stack frame and popped when completed, so when the if block is executed does it gets pushed onto stack frame, or will it get executed in current method stack entry?
Asked
Active
Viewed 201 times
0
-
local variables in the block are allocated on the stack. – OldProgrammer Jun 03 '20 at 18:04
-
2Ask yourself: is an if block a method invocation? Is there a method name involved? Parameters to pass? No? Then it probably doesn't go on the stack (and it is in fact not on the stack). – Joachim Sauer Jun 03 '20 at 18:12
1 Answers
2
An 'if' might have no block, and the block usually has no variable declarations, but if it does, that variable is on the stack (any object it references is, as always, on the heap). It is not a call, so no new stack frame is needed; I'd guess the frame pointer says put and the stack pointer is decremented to make the space for it, perhaps modulo 4 or 8 to keep the CPU happy, and of course in JAVA it is initialized to zero/null, the space location being identified as (frame pointer - N). When the block is over, the stack pointer is incremented back and the name is forgotten.

David G. Pickett
- 387
- 1
- 8
-
The JVM spec doesn't technically talk in terms of "frame pointers", but is uses local variable slots which behave quite similarly. It's notable that the lifetime of each such slot must not match exactly to the scope of the local variable in a method but is dictated by the first and last use of a variable. So it's not as clear cut as "increment a stack pointer when entering an if block". – Joachim Sauer Jun 03 '20 at 18:26
-
Increment when leaving (or last use), decrement on way in ( and zero) to create space, as stack is top of memory down? Of course, jvm is not C but similar! – David G. Pickett Jun 04 '20 at 22:02