public String toString()
{
return "NotSubscribed";
}
We are trying to limit String’s creation in our application after the objects are instantiated. So, just wondering if this creates a new String every time this toString is called. It might be a bad question, but I don’t know the answer. Please suggest.
—
I just remembered that if a String is created, it is stored in the String Pool and after that it is always retrieved from the pool without being re-created. Am I right?
No, that won’t create another string each time. String constants are interned – so actually the constant “NotSubscribed” will refer to the same string object throughout your code, not even just every time this method is executed.
From section 3.10.5 of the Java Language Specification:
However, in your edit:
No, that’s not right. Only string constants are interned by default. For example, every time you run this code:
… that will create another 10 strings.