immutable object点滴

来源:互联网 发布:cytus 剧情 知乎 编辑:程序博客网 时间:2024/06/06 21:04

WIKI:

In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created.

Primitive wrappers (IntegerLongShortDoubleFloatCharacterByteBoolean) are also all immutable. 

可以看到,除了String,其他的包装类其实都是不可改变的。

设计成 immutable的好处有很多。比如简化使用、线程安全等

详情可参考:

http://www.javapractices.com/topic/TopicAction.do?Id=29


可看看String和Integer的源码,class和成员变量基本都是final的,同时不提供改变成员变量的方法。