通过对象输出流改变对象的物理地址,使得地址不同但内容相同

 Boolean a = Boolean.TRUE;

  parseBoolean(Boolean a) ;

 System.out.println(a==Boolean.TRUE); //答案 = false;

 System.out.println(a.equals(Boolean.TRUE));//答案 = true;

private Boolean parseBoolean(Boolean a) throws IOException, ClassNotFoundException {
ObjectOutputStream objectwriter;
ObjectInputStream objectreader;
ByteArrayOutputStream ba = new ByteArrayOutputStream();
objectwriter = new ObjectOutputStream(ba);
objectwriter.writeObject(a);
ByteArrayInputStream bi = new ByteArrayInputStream(ba.toByteArray());
objectreader = new ObjectInputStream(bi);
a = (Boolean) objectreader.readObject();
return a;
}

猜你喜欢

转载自nethub2.iteye.com/blog/2207656