We know that the String object is immutable. But replace method is actually changing its state.
So what is happening in this case?
We know that the String object is immutable. But replace method is actually changing its state.
So what is happening in this case?
That replace method returns a new string after replacing, not the original one.
Look at the source code and see the last line, you'll see that.
return new String(0, len, buf);
Not only replace almost all the methods, returns new string, not the original.