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.
final Foo foo = new Foo(someArg);inside the loop which executed N times, it would construct N separate objects, vs. outside the loop where it would execute once. But if you hadfinal Foo foo1 = new Foo(someArg);outside the loop, and thenfinal Foo foo = foo1inside the loop, you would only have 1 object instantiated. Strings are kind of special, since they are immutable constants, and the compiler would likely optimize it into creating one String object, and re-using it each time in the loop.