-1

When are string literals allocated and put into the string pool? Specifically, if a string literal is never reachable by any code path, will it ever be allocated?

Imagine you have the following code fragment:

    if(logger.isLoggable(Level.FINEST)){
        logger.log(Level.FINEST, "literal");
    }

Will "literal" be ever allocated if FINEST is not and have never been a loggable level?

Aman
  • 735
  • 1
  • 6
  • 19
Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
  • 2
    Please [search throughly](/help/searching) before posting. There are questions with ***very*** thorough answers about how string literals are loaded and handled by the JVM. – T.J. Crowder Aug 31 '17 at 08:29
  • 1
    Maybe this can help https://stackoverflow.com/questions/15596694/memory-allocation-for-local-string-literals – ziMtyth Aug 31 '17 at 08:30
  • @T.J.Crowder I've been searching for the past 3 hours and I didn't find an answer to my question. If know such please link it to the question. – Svetlin Zarev Aug 31 '17 at 08:30
  • @ZiMtyth Thanks, but this does not answer my question, because it discusses the case when the code containing the literals is always reachable, hence they will always be put into the pool. But what will happen if the code containing the literal is never reachable ? – Svetlin Zarev Aug 31 '17 at 08:31
  • And https://stackoverflow.com/questions/39672191/how-is-the-object-created-via-system-out-printlnmorning-reachable/39672247#39672247 and probably several others. – T.J. Crowder Aug 31 '17 at 08:34
  • And https://stackoverflow.com/questions/28250559/when-and-where-is-the-string-initialised-stored-in-java-source-code – T.J. Crowder Aug 31 '17 at 08:35
  • [This answer](https://stackoverflow.com/a/39672247/157247) to the linked question specifically addresses when the string is allocated and whether reachability is a factor. – T.J. Crowder Aug 31 '17 at 08:43
  • @T.J.Crowder Thanks for the links but they provide just *opinions* or current behavior for unknown JVM versiosns without any proof/link to any specification. Andy Turner's answer is what I was looking for. – Svetlin Zarev Aug 31 '17 at 08:45
  • 1
    @SvetlinZarev lack of links to spec != opinion. – Andy Turner Aug 31 '17 at 08:47
  • @SvetlinZarev: You mean like [this answer](https://stackoverflow.com/a/28250587/157247) to one of the questions linked above? – T.J. Crowder Aug 31 '17 at 08:49
  • @T.J.Crowder I don't understand why you are so aggressive ? If you think it's duplicate just link it and close the question. Either way thank you for you effort. I'm out of here. – Svetlin Zarev Aug 31 '17 at 08:54
  • @SvetlinZarev: It's not my intention to be aggressive; written communication can be misleading that way (you're certainly coming across as aggressive, amongst other things, from this perspective -- probably for the same reason). I'm trying to make it clear that searching thoroughly **does** answer this question. I'm sorry if that bothers you, but unnecessary duplication is exactly why sometimes it's hard to find answers. – T.J. Crowder Aug 31 '17 at 08:56

1 Answers1

2

From the JVM spec:

The run-time constant pool for a class or interface is constructed when the class or interface is created (§5.3) by the Java Virtual Machine.

So reachability of the code using it has nothing to do with it.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243