0

Please look at the following Java code:

String greeting = "Hello" + "World";

It's clear that both string literals "Hello" and "World" are created in pools of strings. Still, after this line executes, do they stay them for the entire lifetime of the application even though there is no reference pointing them?

CrazySynthax
  • 13,662
  • 34
  • 99
  • 183
  • Who said they go into the thread pool? And if they do, why would they be removed? – shmosel May 18 '18 at 22:18
  • An object is supposed to be removed if there is no reference pointing to it. – CrazySynthax May 18 '18 at 22:22
  • The string pool isn't garbage collected. – shmosel May 18 '18 at 22:22
  • @shmosel *String pool isn't garbage collected* - are you sure about this? https://stackoverflow.com/a/18407081/303810 – lexicore May 18 '18 at 22:29
  • I guess they can be, technically. But you're not unloading classes, are you? – shmosel May 18 '18 at 22:31
  • 1
    @shmosel *Who said they go into the string pool?* - the [JLS](https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5). – lexicore May 18 '18 at 22:34
  • @lexicore [I think you're mistaken.](https://ideone.com/lSBEua) – shmosel May 18 '18 at 22:54
  • 'It's clear that both string literals "Hello" and "World" are created in pools of strings': no it isn't. This is a compile-time expression. The single literal `"HelloWorld"` is pooled, for as long as the class remains loaded. @lexicore – user207421 May 18 '18 at 23:23
  • The link https://stackoverflow.com/questions/18406703/when-will-a-string-be-garbage-collected-in-java says that every string literal is pooled. "Hello" and "World" are string literals. – CrazySynthax May 18 '18 at 23:25
  • @CrazySyntax But they aren't pooled. The total expression is pooled. See the [JLS link](https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5) provided by lexicore, the part where it refers to #15.28. A StackOverflow answer isn't a normative reference. – user207421 May 18 '18 at 23:27
  • @schmosel You're both mistaken in detail. The fundamental premiss of the question is incorrect. – user207421 May 18 '18 at 23:50
  • @EJP What am I mistaken about? – shmosel May 18 '18 at 23:56
  • @CrazySynthax - I have (ermm) clarified the part of the https://stackoverflow.com/questions/18406703/when-will-a-string-be-garbage-collected-in-java that some people have (ermm) misinterpreted. But ... please ... you should NOT be relying on StackOverflow Q&A's as your definitive source of information. If you want to debate what the Java language spec says, **read** the JLS. Don't be lazy and just rely on someone else's summary. – Stephen C May 19 '18 at 01:08
  • (And in case someone wants to say that the JLS is too hard to understand, I would respond 1) No it isn't ... in this area. 2) No it isn't in general ... if you put in the time. 3) If you don't have the time, you would be wise to stay out if this kind of "language lawyer" debate. Leave it to the people who do have the time / interest / patience to read and understand the JLS.) – Stephen C May 19 '18 at 01:13

0 Answers0