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?
}
Community
  • 1
  • 1
user1301568
  • 2,103
  • 4
  • 25
  • 32

7 Answers7

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.

Juvanis
  • 25,802
  • 5
  • 69
  • 87
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
Community
  • 1
  • 1
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Is that the only difference? – user1301568 Dec 23 '12 at 19:24
  • 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. – Marko Topolnik Dec 23 '12 at 19:28
  • @MarkoTopolnik good point - edited – Bohemian Dec 23 '12 at 19:29
  • 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](http://stackoverflow.com/questions/13531004/java-outofmemoryerror-strange-behaviour) that pointed this out rather dramatically. – Marko Topolnik Dec 23 '12 at 19:40
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.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
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.

Srujan Kumar Gulla
  • 5,721
  • 9
  • 48
  • 78
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,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
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

Community
  • 1
  • 1
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
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