Is it a good practice to create an object inside a loop, I am pointing to the following code: / P>
For (some condition) {SomeClass a = new SomeClass (); Println (a); }
Therefore it will create a new instance of some class for each recurrence. Then the number of examples will be equal to the number of iterations. And then they will be collected later by GC.
Is it better to reuse some class objects inside the loop? Something like this:
some class A = null; For (some condition) {a = new something (); Println (a); }
As far as I understand this, the other way is better because it will only create some class items once and reuse them in every iteration. But I am suspicious, please confirm it, or tell me where my basic is wrong.
The difference is that in your second case, your A
variable still The loop will end when
Besides, they are essentially similar, even from the garbage collection point.
Comments
Post a Comment