Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Object inside a loop

Tags:

Is it a good practice to create an Object inside a loop. I am pointing towards the following code:

for(some condition){
    SomeClass a = new SomeClass ();
    System.out.println(a);
}

So this would create a new instance of SomeClass for each iteration. So number of instances will be equal to number of iterations. And then these will later be collected by GC.

Is it better to reuse a SomeClass object inside the loop. Something like this:

SomeClass a = null;

for(some condition) {
    a = new SomeClass();
    System.out.println(a);
}

As far as I understand this, the second way is better as this will just create the SomeClass object once and will reuse it in every iteration. But I am doubtful. Please confirm this, or let me know where my fundamentals are incorrect.

like image 775
Sambhav Sharma Avatar asked Feb 18 '14 17:02

Sambhav Sharma


3 Answers

The difference is that in your second case, your a variable will still be in scope when the loop is over

other than that, they're essentially the same, even from a garbage collection point of view.

Strings are reference types(albeit immutable ones), and it doesn't really matter whether you declare a new variable for them or just overwrite the same variable each time. You're still creating a brand new string every time.

like image 109
Sam I am says Reinstate Monica Avatar answered Sep 24 '22 08:09

Sam I am says Reinstate Monica


Both create an equivalent amount of strings because String is immutable. Anytime a String is assigned a new value, a new String is created.

Let's assume you meant to use a mutable object in your example.

Option 1

for(some condition)
{
    Object o = new Object();
    System.out.println(o);
}

This will create a new Object o for each iteration of the loop.

Option 2

Object o;
for(some condition)
{
    o = new Object();
    System.out.println(o);
}

This will create a new Object o for each iteration of the loop.

Even for a mutable object, you get the same result either way!

like image 24
Rainbolt Avatar answered Sep 22 '22 08:09

Rainbolt


Be careful not confuse the 'Object' itself and a 'Reference' to an 'Object':

For instance the following code creates a (null) Reference, but no object is created.

Object a = null;

The following code create boths an Object AND a reference to that object (the reference is held in a variable called 'a'):

Object a = new Object();

The following code creates new Object and 'repoints' an existing (reference) variable to point to the new Object: if the variable 'a' already held another reference, 'a' forgots it. [but that doesn't mean other variables may still point to the old object referenced by 'a'].

a = new Object(); // it is the reference 'a' that is 're-used' here not the object...

Everytime you re-run the that statement above in your loop; you are indeed creating a new object ; and you are 're-pointing' 'a' to that new object.

The previous reference (i.e. reference held in 'a') will be forgotten each time; and (assuming we have a single-threaded program here) that means the object it pointed to will have zero references pointing at it now: which means the object is eligible for Garbage Collection. Whether this Garbage collection happens or not at this point in time - I don't know I'm afraid.

But I would say : that there is no difference in your coding examples in terms of when Garbage Collection happens; whether or not the 'pointer-type' is already defined as an 'Object' outside of the loop, or repeatedly redefined within the loop.

The following (useless) examples might help illustrate the difference between the 'Create-an-Object' and 'Point-a-Reference' actions that the code does in one go:

// This creates an object ; but we don't hold a reference to it.
    public class TestClass {
    public static void main(String[] args) {
    for (int n=0;n<100;n++) {
        new Object();
    }
    }
    }

And to contrast:

// This creates a reference ; but no object is created
// This is identical to setting the reference to 'null'.
public class TestClass {
public static void main(String[] args) {
for (int n=0;n<100;n++) {
        Object o;
}
}
}
like image 6
monojohnny Avatar answered Sep 26 '22 08:09

monojohnny