-4

In the code like:

for(int i=0; i<1000000; i++){
    String abc = "blahblahblah";
    abc = abc + foo();
    //save abc to file
}

should I declare abc before this loop, or is the code optimized in a way that make this difference irrelevant?

user3541377
  • 31
  • 1
  • 8
  • 3
    At you moment your re-declaring `abc` each time in the loop, so at the end of the loop it will hardly have changed. – Rudi Kershaw Apr 29 '14 at 08:03
  • In all cases profile to get the answer for your specific case, but due to string interning it *may* not make that much of a difference. For a general object it would definately be better to create it once, outside of the loop. Regardless it isn't abc that should be declared outside the loop but "blahblahblah" – Richard Tingle Apr 29 '14 at 08:05
  • It seems that you need a `BufferedWriter`. – Arnaud Denoyelle Apr 29 '14 at 08:07
  • There is no 'global declaration' here. Adjusted your title. – user207421 Apr 29 '14 at 10:52

1 Answers1

4

If you are only going to use that variable inside of the loop, it is better to declare it inside. That way when you move onto the next iteration of the loop the memory used can be cleared up. Otherwise you would have to wait until the end of the method it was declared in, or for when the Object it is a member of becomes eligible for garbage collection. This is subtly different for primitive variables (as apposed to your String object) which will always be cleared up after the method ends anyway.

In other words, the scope of a variable should always be as small as practically possible to conserve memory (as well as other reasons).

See this answer for more details.

As for performance in speed, there shouldn't be any difference between declaring it inside the loop or outside. As confirmed by bytecode analysis here, and a comprehensive logical analysis here.

I hope this helps.

Community
  • 1
  • 1
Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
  • *"you will end up adding large numbers of new half-way String objects to the String pool"* To the pool? What? Do you just mean 'the heap'? Only String literals and those returned from `intern()` get interned. – Radiodef Apr 29 '14 at 08:48
  • @Radiodef - The `String` pool is an area in the heap where `String` literals are stored. `String` references that are created with literal values that already exist in the pool are re-diverted to the literals in memory that already exist. This is why `String` objects are immutable and why we shouldn't use `==` to compare `String` objects. [Further reading 1](http://www.journaldev.com/797/what-is-java-string-pool), [further reading 2](http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3). Have a read about on google etc, it also called `String` interning. I hope this helps. – Rudi Kershaw Apr 29 '14 at 09:12
  • 1
    Concatenating Strings won't intern them so the last paragraph of your answer doesn't make any sense. Concatenating Strings inside a loop may accumulate a large number of interim objects on the heap but it has nothing to do with the pool. – Radiodef Apr 29 '14 at 09:29
  • @Radiodef - According to the Oracle Certified Programmer Cert specification (written by Oracle), **all** `String` literals are automatically interned (if only briefly, as without a reference they are immediately eligible for GC). You don't have to call `intern()`. In which case, it would or does. – Rudi Kershaw Apr 29 '14 at 09:34
  • 1
    That has nothing to do with what I just said. You should reread it. – Radiodef Apr 29 '14 at 09:40
  • @Radiodef - Updated my answer to specify concatenating `String` **literals** specifically. – Rudi Kershaw Apr 29 '14 at 10:40
  • Concatenating String literals with String variables does not create another String literal and therefore has nothing to do with the String pool. – user207421 Apr 29 '14 at 10:54
  • @EJP - It does. `System.out.println(("a"+"b"=="ab"));` print `true`. – Rudi Kershaw Apr 29 '14 at 10:55
  • 2
    @Rudi That isn't an instance either of what I described or of what the OP is doing. It's irrelevant. – user207421 Apr 29 '14 at 10:55
  • 1
    The OP *is* concatenating String literals with variables. "You will end up adding large numbers of new half-way String objects to the String pool" is therefore incorrect. – user207421 Apr 29 '14 at 11:00