Complete List of immutable JDK classes?

前端 未结 3 2014
北海茫月
北海茫月 2020-12-14 23:49

is there a list of de-facto immutable classes in the jdk?

technically Immutable classes include the obvious Integer, Double etc..

de-facto i

3条回答
  •  無奈伤痛
    2020-12-15 00:34

    Classes whose object contents cannot be modified is called immutable classes. All primitive data types(Wrapper classes only) are immutable. For any class to be immutable the following needs to be done.

    • Make all fields private
    • Don't provide mutators
    • Ensure that methods can't be overridden by either making the class final (Strong Immutability) or making your methods final (Weak Immutability)
    • If a field isn't primitive or immutable, make a deep clone on the way in and the way out.

    Thank you

提交回复
热议问题