WIKI:
In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created.
Primitive wrappers (Integer
, Long
, Short
, Double
, Float
, Character
, Byte
, Boolean
) are also all immutable.
可以看到,除了String,其他的包装类其实都是不可改变的。
设计成 immutable的好处有很多。比如简化使用、线程安全等
详情可参考:
http://www.javapractices.com/topic/TopicAction.do?Id=29
可看看String和Integer的源码,class和成员变量基本都是final的,同时不提供改变成员变量的方法。