dfetter88
dfetter88

Reputation: 5391

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";
}

Upvotes: 18

Views: 18826

Answers (5)

David Weiser
David Weiser

Reputation: 5205

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.

Upvotes: 2

Matt Huggins
Matt Huggins

Reputation: 83309

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.

Upvotes: 7

Falmarri
Falmarri

Reputation: 48615

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.

Upvotes: 0

jzd
jzd

Reputation: 23639

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.

Upvotes: 1

Jason S
Jason S

Reputation: 189876

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.

Upvotes: 26

Related Questions