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"; } 回答1: Is declaring a variable inside of a loop poor practice? Not at all! It localizes the variable to its point