知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

    • 应知

      • Theory:Functional decomposition
      • Theory:Paradigms
      • Theory:Overloading
      • Theory:Write, compile, and run
      • Theory:Annotations basics
      • Theory:JVM, JRE, and JDK
      • Theory:Jave Archive
      • Theory:Running programs on your computer
      • Theory:Enums in Java
      • Theory:Fields and methods in enum
      • Theory:StringBuilder
      • Theory:Immutability
        • Mutability and immutability
        • Custom objects and immutability
        • Summary
      • Theory:Boxing and unboxing
      • Theory:Introduction to generic programming
      • Theory:Generics and Object
      • Theory:What are collections
      • Theory:The collections Framework overview
      • Theory:ArrayList
      • Theory:The List interface
      • Theory:Comparable
      • Theory:Processing strings
      • Theory:Initialization blocks
      • Theory:Introduction to API
      • Theory:Generic methods
    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • 应知
Jim
2022-09-25
目录

Theory:Immutability

In philosophy, there is a thought experiment called "Ship of Theseus". It is one of the oldest concepts in Western philosophy and it goes like this.

The famous ship of the hero Theseus has been kept as a museum piece in a harbor. Over the years, the wooden parts have rotted and been replaced. A hundred years later, all of the wooden parts of the ship have been replaced. The question is: is it still the same ship?

img

There are many ways to answer this question and you can check them out on Wikipedia (opens new window). What is of interest to us is that this question of identity can be applied to programming as well. One of the most important concepts in programming, the one directly connected to change and identity, is the concept of (im)mutability.

In this topic, you will read about the general idea of immutability, and in the following topics, you will explore the means of ensuring immutability in the programming language you have chosen to study.

# Mutability and immutability

Mutability literally means "the quality of being changeable" and, in programming, it refers to the idea of changing the state of the object after it has been created.

Along this line, we can distinguish between mutable and immutable objects. To put it simply, mutable objects can be altered once they've been created and immutable cannot. This is the key difference between mutable and immutable objects.

What does this mean in practice? Immutable objects always represent the same value: if you want to have a different value, you need to create a completely new object. With mutable objects, things are much easier and we can change the values they contain without creating a new object.

Returning to the ship of Theseus, if we were to consider it an immutable object, then the answer to the question of identity would be no. Once you change something, you have a different object. In other words, the ship of Theseus is no longer the ship of Theseus even though it has the same name!

Alternatively, if we regard the ship as a mutable object, then, yes, it is still the same ship. The changes that we made did not affect its identity.

Depending on the programming language you're using, different types of objects may be immutable. For instance, strings are immutable in Python and Java, but Java also has StringBuilder and StringBuffer classes which are mutable. In Ruby and PHP strings are mutable. When writing a program in your favorite language, you need to take into account which objects are mutable and which are immutable in that particular language.

# Custom objects and immutability

In general, objects of custom classes are mutable. However, there are cases when we would want to make them immutable: immutable objects are thread-safe, easier to test, and may be more secure.

提示

Immutable objects can be shared between different threads without additional protection. The state of mutable objects is hard to follow as long as they can be changed by any of the working threads.

In the context of custom objects, we can also talk about weak immutability and strong immutability. Weak immutability is when some fields of an object are immutable and others are mutable. Strong immutability is when all fields of an object are immutable.

Specific instructions on how to make a custom class immutable depend on the language, but we can give general guidelines. Basically, you need to forbid changing the value of the field once it has been created or forbid reassigning the value. This can be done, for example, by making the field read-only or a constant. Another option is to modify the methods that set attribute values so that they throw exceptions. You can also work with access modifiers: make the fields unattainable from the outside of the class.

# Summary

To sum up, the difference between mutable and immutable objects lies in the fact that mutable objects can change their states after creation and immutable objects cannot. Languages have their own division into mutable and immutable objects. Custom classes are usually mutable but can be made immutable using language-specific tools and techniques if necessary.

编辑 (opens new window)
#Java
上次更新: 2022/09/26, 16:55:15
Theory:StringBuilder
Theory:Boxing and unboxing

← Theory:StringBuilder Theory:Boxing and unboxing→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式