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";
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…