Originally posted by Jim Frank:
So the literal String in the first example will be garbage collected, unlike a literal?
What do you mean? If you create a new String it will be eligible for collection when there's no longer a reachable reference to it. However, the actual literal "" will never be collected, it will remain in the literal pool. The distinction between creating it with a variable initializer and returning it from a method is irrelevent. If it's not a literal, and in your case it's not because you explicitly created a new String, then it can become eligible for collection.