JAVA: the link defined in the loop

I'm just curious.
Suppose I define a link inside a while / for loop.

Does the JVM support this link at each iteration, or is it optimized to determine it only once?

+7
source share
4 answers

It defines each time and covers only this iteration of the loop.

As soon as the loop repeats, it is entitled to the GC.

As Louis Wasserman commented, the variable will be re-initialized each time, but the memory space is likely to be reused.

+7
source

The link is set at each iteration. Once the code has been optimized for native code, it can be moved outside the loop so that it does not affect performance. If you set this link to the new object each time, it can create a new object at each iteration, if it is also not optimized.

+4
source

It is determined every time. There is no optimization for this (as far as I know).

+2
source

- defines everytime link , the loop repeats, but the scope of the link is tied only to this iteration.

- Note that you have declared a link outside the loop , but assign an object inside the loop, the link remains the same, but it refers to a new object of this type at each iteration.

+1
source

All Articles