Local class method, variables, let me ask you a question about the restriction on the use of Java.

Asked 1 years ago, Updated 1 years ago, 99 views

According to the book, when the method ends, it is removed from the stack frame Local class instances remain in the heap area and become a problem when using local variables or parameters in the method To solve the problem, it is described as using a capture of the variable at that time, like JavaScript So it's supposed to be declared final

Can the remaining objects in the heap area be used again?

I don't understand!

java jvm

2022-09-22 14:26

2 Answers

Please transfer the contents of the book as it is.

final is a constant, read-only, or non-correctable type of declaration. Declaring final does not create on the stack.

The jvm is managed by a garbage collector. There is no way for developers to intervene.(Excluding System.gc calls, of course)

If it is a generic algorithm among the gc algorithms in jvm, it is basically a copying algorithm and we organize garbage with mark and sweep.

I understand that there is no way to cancel and recycle the mark that is supposed to be deleted.

Sweep garbage after marking and process it at once.

Of course, we don't mark objects that are referenced somewhere.


2022-09-22 14:26

I think you're talking about the closure, but the closure of Java is implemented differently than the closure of JavaScript. It's much more restrictive than JavaScript.

When an internal object captures an external variable, Java copies the value, not the reference, to the external variable. So, the external variable is in the external variable, and you copy it exactly as it is, and you get it into the area of the internal object. The problem with this is that even if you modify your own variables inside, or if you modify your own variables outside, you can't synchronize with each other. It's just a different variable now that it's been copied. But developers don't accept it that way, and they've modified the external variable from within, so they'll expect it to be applied from outside. However, because the implementation is not like that, we made it final when referring to external variables from within. It was forced to be finalized, so it was made impossible to modify it at all from the outside and inside.

In contrast, JavaScript does not import values, but imports references to variables. You can change the value from the outside, you can change the value from the inside, and if you change the value, the changed value is retained. I don't think it's necessary to mention JavaScript, but I'm adding that JavaScript is mentioned in the text.


2022-09-22 14:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.