开发者

Java - Declaring variables in for loops

开发者 https://www.devze.com 2023-01-31 20:50 出处:网络
Is declaring a variable inside of a loop poor practice? It would seem to me that doi开发者_StackOverflow中文版ng so, as seen in the first code block below, would use ten times the memory as the second

Is declaring a variable inside of a loop poor practice? It would seem to me that doi开发者_StackOverflow中文版ng 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";
}


Is declaring a variable inside of a loop poor practice?

Not at all! It localizes the variable to its point-of-use.

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

The compiler may optimize things to keep memory use efficient. FYI: you can help it, if you use the final keyword to tell it that your variable has a fixed reference to an object.

Note: if you have a more complex object where you are executing complex code in the constructor, then you may need to worry about single vs. multiple executions, and declare the object outside of the loop.


In both examples, you're going to instantiate a new string object that contains the string "Some String" the same number of times.

In the first example where you declare str inside of the loop, all references to that string are going to be lost after the for-loop completes, allowing Java's garbage collector to remove all instances of the strings from memory. However, in the second example where you declare str outside of the loop, the last string you created will still have a reference to it outside the loop, and Java's garbage collector will only remove 9 out of 10 of the strings from memory that were instantiated.

As such, the first method is better since you do not hold onto any references of the string, interfering with the garbage collector's ability to determine if it's still in use.


Beyond what @Jason S said, I'd also encourage you to think about the readability of the code.

For instance, if you are only writing to a reference once, it would make your intent more clear to use something like:

String str = "write once";
while(condition){
    //do stuff with str
}

Versus:

String str = null;
while(condition){
    str = "write once";
    //do stuff with str
}

Likewise, if the value of the string is really based off something that is specific to an iteration of the loop, declare the variable inside the loop.


The memory used by a variable reference is small. It is typically better practice to declare the item inside the loop because it will be closer to where it is used and more readable.


It depends. The inefficiency is from the overhead creating the String object, assuming your compiler doesn't change anything. The memory will be cleared once it goes out of scope.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号