give me please examples where I could see advantages of immutable objects. Info I found in internet are concentrated in threads. I don't know about threads yet. would be great if the examples would use simple principles
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
A good example is the
String
class:A good summary of reasons for immutable objects can be found here
As Java returns by value (i.e. object references are returned from methods), if for example I return a String like so:
and the String class wasn't immutable, then the caller of
getString()
could modifymyString
, which may not be the desired behaviour; other parts of the system might not want or expectmyString
to change. So the caller can only change the object whichmyString
points to, notmyString
itself.