附代码;
package test.bean;
import java.io.Serializable;
public class SerializableBean implements Serializable{
private static final long serialVersionUID = 1L;
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
复制代码
package test.trys;
import java.util.ArrayList;
import java.util.List;
import test.bean.SerializableBean;
public class TryTest {
public static void main(String[] args) {
changeObject();
changeString();
}
private static void changeString() {
List<String> l = new ArrayList<String>();
String a = "SS";
l.add(a);
System.out.println(l.get(0));
a = "zzz";
System.out.println(l.get(0));
}
private static void changeObject() {
List<SerializableBean> l = new ArrayList<SerializableBean>();
SerializableBean e = new SerializableBean();
e.setTest("SS");
l.add(e);
System.out.println(l.get(0).getTest());
e.setTest("zzz");
System.out.println(l.get(0).getTest());
}
}
复制代码
他的输出结果是什么?
为什么是这样的输出结果,如果是线程在更改字段会引发怎么样的问题。 |