On various specialized Internet resources there are many opinions on this issue. Sometimes these opinions are diametrically different from each other. For example, there is a whole layer of practitioners who do not strongly recommend doing so (mainly from the community of programmers who work with the C / C ++ language). Others advocate that variables be declared as “locally” as possible. What practice, in your opinion, is more justified?

Moving on ... I would like to understand what happens to the stack when we declare a variable in a loop. Do I understand correctly that after each iteration passed, those local variables that were declared directly in the body of the loop itself go through the removal process from the stack (pop operation is performed), and then they are loaded again (this happens during the new iteration, In this case, the push operation is triggered? I'm not sure that this is the case, but the following test code brought me to such reflections:

class Test { public static void main(String[] args) { for(int i = 0; i < 5; i++) { if(i == 1) System.out.println(j); int j = 0; } } } /* Compilation Errors Detected Line: 6 cannot find symbol symbol: variable j location: class Test */ 

As we can see, the compiler indicates to us that within the scope in which we are trying to access our local variable, there is actually no variable with such an identifier (although we already declared it in the previous iteration). What else confuses me is that if you look at the byte-code if the variable is specified outside the cycle (but will be used in it) and the similar binary code for the situation when the announcement took place in the cycle, it turns out that they absolutely the same!

Now the key question. Given all the above, what is really happening?

  • 2
    1) You write in Java, why do you listen to those who write in C / C ++? these are completely different PLs with different behaviors with different engine actions. 2) What actually happens is exactly what you see in the byte code. 3) Well, now it is more justified to use suitable variables for current needs . if you need a local variable - use a local variable, need a global one - use a global one. - Vladimir Klykov
  • And with a stack what manipulations are performed? Is the variable loaded only once? - Lexoid
  • one
    4) Of course, there are always tricks, but it’s too early for you to think about them, you first need to learn how and exactly how your PL, its optimizer, internal structures work, and only after that can you understand the tricks of optimization. - Vladimir Klykov
  • 2
    I am answering your comment, you don’t need to know it now, what will change this knowledge for you now? Can you use it properly? do you know well the VM Java architecture? ... Shl. All my advice is from the category of general, I am not a Java programmer, and certainly I don’t know its sub-standard. And yes, I’ll give you a little light - how exactly the variable will be stored depends very much on how it will be used; modern YaBs are very good at optimizing the code - Vladimir Klykov
  • one
    Why know this? I want to satisfy my curiosity! Sorry, I am such a man by nature. If there is a question in my head and I can’t find an answer to it, then sometimes I can’t even fall asleep!) - Lexoid

1 answer 1

Memory for stack is allocated once. The compiler calculates the maximum necessary memory and allocates as much as necessary.

 void foo() { if(condition1) { int i = 1; } if(condition2) { int j = 2; } } 

In this example, only one variable will be allocated on the stack.

The pop operation (or rather, its analogue) is called only once upon exiting the method.

Your example with a compilation error is explained by the fact that the compiler ensures that local variables are initialized.

  • That is, if we declare a variable in a loop, then memory allocation for it will occur only 1 time? - Lexoid 4:14 pm
  • @Lexoid Exactly. The memory on the stack is allocated once to call the function. - talex