String s = "java";
s.substring(1); // ava
Considering the immutability of strings, compiler doesnot modify 's' but creates a new object or you can say that there is space for 'java' as well as 'ava' in memory..
What happens to this 'ava', as nothing is pointing to it or it’s not being referenced by anything..
One more question… If i would have written String s = new String("java"); // ‘java’ is not in string literal pool….
The ‘ava’ would be in the string literal pool then or not ?
The newly constructed
Stringobject immediately becomes eligible for garbage collection.As to your second question, I don’t think there’s any difference between the following two expressions as far as the string literal pool is concerned:
In both cases, the literal
"java"would be in the pool. (But in the second case,swould not refer to the"java"string that’s in the pool, but to a differentStringthat also has the characters"java".)