封装类为何是immutable 不可改变的

1. 问题引入demo

package com.fqyuan.Wrapper;

import java.util.concurrent.atomic.AtomicInteger;

public class TestWrapper {

    public static void main(String[] args) {
        Foo f1 = new Foo();
        Foo f2 = new Foo();
        Foo f3 = new Foo();
        AtomicInteger i = new AtomicInteger(1);

        f1.setValue(i);
        i.incrementAndGet();
        f2.setValue(i);
        i.incrementAndGet();
        f3.setValue(i);

        Integer j = new Integer(1);
        f1.setItem(j);
        ++j;
        f2.setItem(j);
        ++j;
        f3.setItem(j);
        System.out.println(f1.getValue() + " " + f2.getValue() + " " + f3.getValue());
        System.out.println(f1.getItem() + " " + f2.getItem() + " " + f3.getItem());
    }
}

class Foo {
    /*
     * The primitive type are mutable, but they are not sharable- That's why no
     * 2 sections of code will ever be referring to the same int variable (they
     * are always pass by value). So you can change your copy and no one else
     * sees the change, and vice versa.
     */

    private AtomicInteger value;
    private Integer item;

    /*
     * If strings and wrapper classes are non-final, anybody can extend those
     * classes and write their own code to modify the wrapped primitive data.
     * So, in order to maintain data integrity, the variables which we are using
     * for data storage must be read-only. i.e., Strings and Wrapper classes
     * must be final & immutable and 'pass-by-reference' feature should not be
     * provided.
     */
    public void setValue(AtomicInteger value) {
        this.value = value;
    }

    public AtomicInteger getValue() {
        return value;
    }

    public Integer getItem() {
        return item;
    }

    public void setItem(Integer item) {
        this.item = item;
    }
}
//Running result:
3 3 3
1 2 3

为何上述结果是:对于AtomicInteger是相同的,而对于Integer类型的是不同的值呢?

2. 原因总结

下面原因部分来自于此处的总结。
1. 首先primitive数据类型本身不属于对象,故而讨论其是否mutable没有意义。
2. 其次,String不属于Java语言基本数据类型,它属于引用数据类型,即:它是属于一种Object。但它和Wrapper 类一样,都是不可改变的,所以有一些共性。
3. primitive数据类型是不可共享的,即:没有任何2块代码引用同一个int 变量值,Java语言特性:pass by value. 是以值的一份副本传递的,故而对于其所做的修改并不会对其他使用该值的函数可见。
4. 如果String和Wrapper 类 non-final的,那么,任何人都可以继承该类,并对类中数据wrapped primary data进行修改。所以,为了维持数据一致性,所以我们用于数据存储的数据类型必须是read-only的。也就是说:String & Wrapper类必须是 final & immutable的,“pass-by-reference”(注意Java中其实只有pass by value说法,但是对于引用数据的修改,其作用结果就像是传递了引用一样,其实传递的依然是值,只是这里的值是引用对象的copy) 特性不应当提供给该类数据。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值