0

If I pass in a String literal as an argument, I assume that the String literal is made in the String pool right as a String object? Does this this mean that the literal is somewhere in the heap without a reference of course? And once the method is done executing would the String in the pool be gone?

The same question I would like to ask for primitive type literal values, are they just put on the stack when the method is executed and then they are removed from the stack once the method is done executing?

bayer
  • 1
  • 2
    Does this answer your question? [string literals as argument to methods](https://stackoverflow.com/questions/11169641/string-literals-as-argument-to-methods) – Harmandeep Singh Kalsi Jul 20 '20 at 05:18
  • Check more explanation of your question in https://stackoverflow.com/questions/8046045/java-string-literal-pool-and-string-object – Ashish Karn Jul 20 '20 at 05:21
  • Any object created in heap(String as well) is collected(removed) by GC, primitives created in Thread stack will be removed as soon as the method execution is complete – Nitin Dandriyal Jul 20 '20 at 05:40
  • So for primitive literals they are removed right after the method is done executing from stack but for String literals they will last inside the String pool in the heap for the whole execution of the program, not just the method alone? – bayer Jul 20 '20 at 06:22

0 Answers0