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) 特性不应当提供给该类数据。