example code:
StringBuffer sb = new StringBuffer("hi");
sb = null;
Question:
will the literal string "hi" somehow stay in memory, even after the StringBuffer has been garbage collected? Or is it just used to create a char array for the StringBuffer and then never put anywhere in memory?
StringBuildertoStringBuffer. As for the literalString, it depends on your version of Java and whether or not it's in theinternpool.StringBuilder. After all, you have to decide for either.StringBufferwill copy the argument passed to its constructor, but does not hold any reference to it, hence, whether it gets garbage collected or not, is entirely irrelevant to the life cycle of the"hi"string instance.