Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: instantiate variables in loop: good or bad style?

Ive got one simple question. Normally I write code like this:

String myString = "hello";

for (int i=0, i<10; i++)
{
  myString = "hello again";
}

Because I think the following would not be good style cause it would create too many unnecessary objects.

for (int i=0, i<10; i++)
{
  String myString = "hello again";
}

Is this even correct? Or is this just the case when Ive got an explicit object like an object from a class I created? What if it was a boolean or an int? What is better coding style? Instantiate it once before the loop and use it in the loop or instantiate it every time in the loop again? And why? Because the program is faster or less storage is used or...?

Some one told me, if it was a boolean I should instantiate it directly in the loop. He said it would not make a difference for the heap and it would be more clear that the variable belongs inside the loop. So what is correct?

Thanks for an answer! :-)

====

Thanks for all your answers!

In conclusion: it is preferable to declare an object inside the smallest scope possible. There are no performance improvements by declaring and instantiating objects outside the loop, even if in every looping the object is reinstantiated.

like image 492
nano7 Avatar asked May 09 '12 19:05

nano7


4 Answers

No, the latter code isn't actually valid. It would be with braces though:

for (int i=0; i<10; i++)
{
    String myString = "hello again";
}

(Basically you can't use a variable declaration as a single-statement body for an if statement, a loop etc.)

It would be pointless, but valid - and preferable to the first version, IMO. It takes no more memory, but it's generally a good idea to give your local variables the narrowest scope you can, declaring as late as you can, ideally initializing at the same point. It makes it clearer where each variable can be used.

Of course, if you need to refer to the variable outside the loop (before or afterwards) then you'll need to declare it outside the loop too.

You need to differentiate between variables and objects when you consider efficiency. The above code uses at most one object - the String object referred to by the literal "hello again".

like image 142
Jon Skeet Avatar answered Oct 30 '22 00:10

Jon Skeet


As Binyamin Sharet mentioned, you generally want to declare a variable within the smallest scope possible. In your specific examples, the second one is generally preferable unless you need access to the variable outside your loop.

However, under certain conditions this can have performance implications--namely, if you are instantiating the same object over and over again. In your particular example, you benefit from Java's automatic pooling of String literals. But suppose you were actually creating a new instance of the same object on every iteration of the loop, and this loop was being executed hundreds or thousands of times:

for (int i=0, i<1000; i++)
{
  String myString = new String("hello again"); // 1000 Strings are created--one on every iteration
  ...
}

If your loop is looping hundreds or thousands of times but it just so happens that you're instantiating the same object over and over again, instantiating it inside the loop is going to result in a lot of unnecessary garbage collection, because you create and throw away a new object on every iteration. In that case, you would be better off declaring and instantiating the variable once outside of the loop:

String myString = new String("hello again"); // only one String is created

for (int i=0, i<1000; i++)
{
  ...
}

And, to come full circle, you can manually limit the scope by adding extra braces around the relevant section of code:

{ // Limit the scope
  String myString = new String("hello again");

  for (int i=0, i<1000; i++)
  {
    ...
  }
}
like image 28
rob Avatar answered Oct 30 '22 02:10

rob


Seems like you mean declare, not instantiate and in general, you should declare a variable in the smallest scope required (in this case - in the loop).

like image 1
MByD Avatar answered Oct 30 '22 01:10

MByD


if you are going to use the variable outside the for loop, then declare it out side, otherwise its better to keep the scope to minimum

like image 1
Habib Avatar answered Oct 30 '22 01:10

Habib