Rajat Sharmanaive wrote:Has it got anything to do with immutability.
Agree. When you counted the void methods which alter Strings, you didn't find any. There are two void methods, but those put part of the String into a buffer, so they don't change the String either. All the other methods which create a new String have String as their return type. Which would all lead you to believe String is immutable . . . if you didn't see
Matthew Brown wrote: . . .
Spot on. . . .
which is the second line of the description part of the String documentation. You can write things like s.concat("Campbell") but you can't use = so the catenated String vanishes into cyber-Limbo never to be seen again.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. . . .
This is very insightful. Never noticed.
you can change the state of an object in an array or Iterable using any of the object's public interface.
Indeed Campbell. It is sinful to expose variables like this. Didn't pay attention.
(Actually your example shows poor design; it would be better to say Dog.setName("Fido")