2

Possible Duplicate:
Difference between declaring variables before or in loop?

String str;
for (int i = 0; i < 10; i++) {
    str = "Hello, World"; // Is str created only 1 time?
}

What is difference between above and below? And if they are different, which one is better?

for (int i = 0; i < 10; i++) {
    String str = "Hello, World"; // Is str created 10 times?
}
2
  • 1
    both creates 10 strings. Commented Dec 23, 2012 at 19:24
  • 3
    @Subin only if you don't account for string interning. Commented Dec 23, 2012 at 19:25

7 Answers 7

3

In the first example, the variable can be used out of the scope of for-loop, whilst the other one is just visible inside the for-loop.

Sign up to request clarification or add additional context in comments.

Comments

2

If you didn't write up your example with a string literal, which is basically a singleton constant, then the answer would be that in both cases 10 objects are created. In your specific example, no objects are created.

Comments

2

The difference is you can access the str variable after the loop in the first example.

Scope:

The scope of the variable is different:

  • The variable defined outside the loop is accessible anywhere the method after it is declared (and initialized), including inside the loop
  • The variable defined inside the loop is only accessible inside the loop

Initialization:

If your loop was possible to not iterate, the variable would be uninitialized in the first example, so you couldn't use it after the loop until you gave it a value (even if that value is null)

Garbage collection:

The point at which it is available for garbage collection is:

  1. After the last usage within the method for example 1,
  2. At the end of the loop for example 2

4 Comments

Is that the only difference?
whether it will be GC-able anywhere before the method end is not specified and in practice, the value last assigned may well hang on till the method completes.
@MarkoTopolnik good point - edited
No, actually in the second case, even when it leaves its scope, there is still no guarantee that it will be GC-able before the method completes. There was a question that pointed this out rather dramatically.
1

Since it's a string literal its not going to be created 10 times. It's going to be saved in permgen space and not heap space.

Comments

0

In Java, string constants are interned, meaning that the same string literal is only kept in (PermGen) memory once, such that eg.:

"foo" == "foo"

is idempotently true. Thus, both your snippets will be equal in memory footprint, but with the first, you can use str after the loop terminates.

Whether one or the other is better, that really depends on what you need to do with str after the loop.

Cheers,

Comments

0

From a maintenance perspective, second is better. Declare and initialize variables in the same place, in the narrowest scope possible. Don't leave a gaping hole between the declaration and the initialization, and don't pollute namespaces you don't need to.

Refer to this link

Comments

0

In the first case a variable global to the loop is initialized with a value whos is reference is changed 10 times whereas in the second case a variable local to the loop is re-initialized 10 times with the same value

Note:There is only one variable and not 10 in both case

Better use an array, vector or linked list for the purpose of multiple variable initialization

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.