1

We know that the String object is immutable. But replace method is actually changing its state.

So what is happening in this case?

1 Answers1

2

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.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307