java - Extra variable or existing variable -


I want to ask that there is any effect (such as performance) between these two types of code.

Method 1:

  object object = new object (); // different variable object object2 = new object (); . . .  

Method 2:

  object object = new object (); . . Object = new object (); // Repeat the same variable  

Is it different from coding style or any other reason, such as reserve memory, better performance?

In Method 1, you are creating two references and collecting two different objects . Both objects are not eligible for garbage collector.

On Method 2, you are creating a reference and you can allocate a new object and then you create a new object and allocate it in context. Now there was no reference in your first object, so this garbage collector is eligible.

Good luck !!!


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -