不可变JDK类的完整列表? [英] Complete List of immutable JDK classes?

查看:98
本文介绍了不可变JDK类的完整列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jdk中是否有事实上不可变类的列表?

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

技术上不可变类包括明显的Integer,Double等。 。

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

事实上,不可变的将包括例如java.lang.String - 它在技术上可能是可变的,但实际上它不是。

de-facto immutable will include for example java.lang.String - it might technically be mutable but de-facto it is not.

此外,是否有必要的接口/抽象类(如javadoc中所述)是不可变的?

Also, are there Interfaces/Abstract classes which are required (as stated in the javadoc) to be immutable?

如果你不能提供完整的List,如果你知道一堆在javadoc中声明不变性的类,我会很高兴。

if you cannot provide a complete List, i would already be happy if you know a bunch of classes which state immutability in its javadoc..

推荐答案

无法修改的对象内容称为不可变类。所有原始数据类型(仅限Wrapper类)都是不可变的。对于任何不可变的类,需要进行以下操作。

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.


  • 将所有字段设为私有

  • 不提供变异器

  • 确保无法通过使类
    final(强不可变性)或使您的方法成为最终(弱
    不变性)来覆盖方法)

  • 如果某个字段不是原始字段或不可变字段,请在
    in和out out的路上进行深度克隆。

  • 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.

谢谢

这篇关于不可变JDK类的完整列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆