Java - Declaring variables in for loops

Is declaring a variable inside of a loop poor practice? It would seem to me that doing so, as seen in the first code block below, would use ten times the memory as the second... due to creating a new string in each iteration of the loop. Is this correct?

for (int i = 0; i < 10; i++) {
  String str = "Some string";
}

vs.

String str;
for (int i = 0; i < 10; i++) {
  str = "Some String";
}
16
задан Tom Hawtin - tackline 21 December 2010 в 18:41
поделиться