封装类为何是immutable 不可改变的
来源:互联网 发布:如何在linux下编程 编辑:程序博客网 时间:2024/05/22 02:14
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 31 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
- 封装类为何是immutable 不可改变的
- String 类是不可改变的解析
- 为何String对象是immutable或final的?
- Java String是不可变的(immutable)
- 不可变类(immutable class)
- Java Immutable不可变类
- 【Java】【容易忽略的知识点】不可变类 (IMMUTABLE CLASS)
- JAVA中的String为何是不可变的
- JAVA的不可变类(immutable)机制与String的不可变性
- JAVA不可变类(immutable)机制与String的不可变性
- JAVA不可变类(immutable)机制与String的不可变性
- JAVA不可变类(immutable)机制与String的不可变性
- JAVA不可变类(immutable)机制与String的不可变性
- 不可变类:immutable 与 可变类
- 不可变类(immutable class)
- 直接改变环境变量PATH,是不可以更换JDK的
- 6.7String是内容不可改变,还是对象不可改变
- 怎么理解字符串对象是不可改变的对象?字符串引用变量的值可以改变?
- 欢迎使用CSDN-markdown编辑器
- Codeforces 834 A The Useless Toy
- yii2.0 Html助手和Request组件
- QSystemTrayIcon 系统托盘使用总结
- InnoDB关键特性之自适应hash索引
- 封装类为何是immutable 不可改变的
- Java动态代理机制详解(JDK 和CGLIB,Javassist,ASM)
- VS2010 error C2061: 语法错误: 标识符“__RPC__out_xcount_part” 解决方法
- Jconsole连接远程Jetty
- eclipse 导出war包时报svn更新状态失败问题
- Linux 应用开发
- Flume基本使用
- 关于NavigateUrl中绑定Eval()方法时出现"服务器标记的格式不正确"的解决方法
- Circular placeholder reference 'jdbc.driver' in property definitions