Lecture 9: Mutability & Immutability

1 Mutability

Mutable : have methods that change the value of the object
Immutable : Once created, they always represent the same value.

  • A common use for it is to concatenate a large number of strings together.
  • Another is convenient sharing: two parts of your program can communicate more conveniently by sharing a common mutable data structure.
1.1 Risks of mutation
  • The answer is that immutable types are safer from bugs, easier to understand, and more ready for change.
  • Immutable types are safer from bugs, easier to understand, and more ready for change.
    • Passing mutable objects around as parameter **or **return value is a latent bug.
    • never use Date Class. Use one of the classes from package java.time : LocalDateTime , Instant , etc. All guarantee in their specifications that they are immutable.
  • Aliasing(Mutiple reference) is what makes mutable types risky.
1.2 Specifications for mutating methods
  • At this point it should be clear that when a method performs mutation, it is crucial to include that mutation in the method’s spec.

2 Iterating over arrays and lists

  • Iterator is an effective design pattern.
  • If you want to modify a collection while iterating, use iter.remove() method instead of collection.remove(), because iter.remove() is more efficient and can adjust the index of the collection. For example:
Iterator iter = subjects.iterator();
while (iter.hasNext()) {
    String subject = iter.next();
    if (subject.startsWith("6.")) {
        iter.remove();
        //do not call this mehod:
        //subjects.remove(subject);
    }
}

3 Some Useful immutable types

  • The primitive types and primitive wrappers are all immutable.
  • Don’t use mutable Date s, use the appropriate immutable type from java.time based on the granularity of timekeeping you need.
  • Collections also provides methods for obtaining immutable empty collections: Collections.emptyList , etc.
  • The usual implementations of Java’s collections types — List , Set , Map — are all mutable: ArrayList , HashMap , etc. The Collections utility class has methods for obtaining unmodifiable views of these mutable collections:
    • Collections.unmodifiableList
    • Collections.unmodifiableSet
    • Collections.unmodifiableMap
      You can think of the unmodifiable view as a wrapper around the underlying list/set/map. A client who has a reference to the wrapper and tries to perform mutations — add , remove , put , etc. — will trigger an UnsupportedOperationException.

Reference

[1] 6.005 — Software Construction on MIT OpenCourseWare | OCW 6.005 Homepage at https://ocw.mit.edu/ans7870/6/6.005/s16/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值