1

I would like to know which of the following should be used:

String s = "";
List<String> list = new List<String>();
for(int i=0; i<100; i++){
    s = list.get(i);
    list.add(s);
}

or

List<String> list = new List<String>();
for(int i=0; i<100; i++){
    String s = list.get(i);
    list.add(s);
}
Claus
  • 150
  • 8
Teeranai.P
  • 119
  • 1
  • 4
  • 9

4 Answers4

9

Nowadays, it does not matter. The compiler will perform several optimizations and the final code will be equivalent. There is a best practice that says the scope of local variables should always be the smallest possible. In this case, it's better to declare your variable inside the loop.

I suggest you to take a look at this post.

Community
  • 1
  • 1
Trein
  • 3,658
  • 27
  • 36
1

From here:

The answer is emphatically that str absolutely ought to be declared within the while loop. No ifs, no ands, no buts.

The only case where this rule might be violated is if for some reason it is of vital importance that every clock cycle must be squeezed out of the code, in which case you might want to consider instantiating something in an outer scope and reusing it instead of re-instantiating it on every iteration of an inner scope. However, this does not apply to your example, due to the immutability of strings in java: a new instance of str will always be created in the beginning of your loop and it will have to be thrown away at the end of it, so there is no possibility to optimize there.

There's is a difference if the variable is a primitive or an object as explained with great detail here:

for primities... variables declared outside the loop you can do it with no real fear of a performance hit. If you like it declared outside the loop and want the best possible performance you can declare it outside and not initialize it.

for objects... If an object is declared inside a loop the memory for the object is allocated each time and the initialization for the object is performed. The initialization may not take that much time but the memory allocation will be. In some cases you may not be able to get beyond creating a new object with every loop but if possible it is better to reset the object and reuse it.

Community
  • 1
  • 1
Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • The second link is broken. This seems to be correct one now: https://community.oracle.com/blogs/ddevore/2006/08/21/declare-variables-inside-or-outside-loop – uutsav Jun 28 '17 at 09:06
0

I would say it is more a designed choice.

Do you need access to the variable outside the context of the loop? If so the former is correct and if you only need it for processing in the loop then the latter.

I am not expert but I would say if you do the first option and then do not use there is a chance you could delay GC. Although, GC normally doesn't happen anyway until you move out of the context.

JodiMiddleton
  • 310
  • 1
  • 8
0

In the case with String, I don't think that it will impact performance since String immutable. The only "big" thing is that you change the pointer of s in the first, and in the second you create a new pointer every time. But as Rohit Jain commented, if you are not going to use s outside of the loop, go with the second.

If you where dealing with a mutable type, I would go with the first one (depending on the rest of code, and some other meta stuff)

Nicolai Krüger
  • 416
  • 4
  • 17