1

Consider the code

String s="hello";
s=s.concat("java");

As String objects are immutable so new object should be created with value “Hello Java” referenced by S.

Now my query is that where is that earlier String object “hello” referenced by s initially gone?

Does it still exist in memory? if so then how? because in Java the life time of object remains only till it’s reference exist. In case of “hello” it’s reference had lost when second statement executed. so how can it still exist in memory???

Please someone solve my doubt. :)

Andy Turner
  • 122,430
  • 10
  • 138
  • 216
Protagonist
  • 1,390
  • 6
  • 26
  • 49
  • 6
    "Does it still exist in memory?" Yes. You just don't have any way of accessing it any more. It will continue to exist in the constant pool, since you have declared it as a string literal; even if it were not in the constant pool, it would continue to exist until it is GC'd. – Andy Turner Jan 26 '16 at 13:59
  • All string constants are [interned](https://en.wikipedia.org/wiki/String_interning) – Bohemian Jan 26 '16 at 14:02

1 Answers1

8

Does it still exist in memory?

Yes. The s variable just doesn't reference it any more.

It will continue to exist in the constant pool, since you have declared it as a string literal; even if it were not in the constant pool, it would continue to exist until it is garbage collected, which will not happen immediately.

Andy Turner
  • 122,430
  • 10
  • 138
  • 216
  • 6
    you can access it again by `String t = "hello"` – wero Jan 26 '16 at 14:03
  • @AndyTurner: Wasnt this one of the ways to [create a memory leak](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java) with java? – hamena314 Jan 26 '16 at 14:03
  • @Saba: Garbage collector ... it cleans the memory – hamena314 Jan 26 '16 at 14:04
  • 1
    @wero - yep. I've used more careful wording now. – Andy Turner Jan 26 '16 at 14:05
  • @wero - I've you *even more* careful wording, since you could, obviously, have another variable referencing it in code not shown here. – Andy Turner Jan 26 '16 at 14:06
  • @AndyTurner- Is it not a memory overhead to keep that? – Protagonist Jan 26 '16 at 14:07
  • @Protagonist of course - storing something in memory takes more memory than not storing something in memory. But you don't have control over when Java cleans it up (other than not declaring it as an interned/literal string, and even then it is a pretty crude control). – Andy Turner Jan 26 '16 at 14:09
  • @hamena314 I'm not sure exactly what you are referring to. If you are thinking about interning as the means to create a leak, nothing is being explicitly interned. – Andy Turner Jan 26 '16 at 14:12
  • @AndyTurner: I thought about it and I think, altough the reference is gone, it will still exist in the constant pool - which can be reached by the GC and therefore no memory leak will happen? – hamena314 Jan 26 '16 at 14:18
  • 2
    @hamena314 it stays in constant pool because it is a constant in the code. That is not a leak at all and unless the code contains so many constants that it can fill memory with those, it is not a problem either. You can however "leak" memory by a poor log/cache implementation. – Vlasec Jan 26 '16 at 14:20