首页 > 代码库 > java序列化之后,对象的引用关系?

java序列化之后,对象的引用关系?


今天写代码的时候用到序列化,不过突然想到这个问题。

于是写了一些测试代码,得出两个结论。


如果两个对象存在引用关系,比如A引用B。

如果两个对象是各自序列化的,则引用关系不再存在。

如果两个对象是是另一个类对象C的成员,序列化C,反序列化C之后,A和B的引用关系还存在。


我就是做了这个两个实验。

1 node2引用了node1,node2.parent=node1,

序列化和反序列化之后,

发现node2.parent !=node1。

node2.parent 指向的内存跟node1不同,而是另外在内存生成了一个对象。

2 Tree有两个成员,node1和node2,

node2引用了node1,node2.parent=node1,

序列化和反序列化之后,

node2.parent =node1。



数据结构定义:

class node  implements Serializable
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8475920991534073160L;
	node parent;
	int id;
	public node(int i)
	{
		id = i;
		parent= null;
	}
}

//nodeTree,node1和node2存在引用关系
class nodeTree implements Serializable
{
	node node1;
	node node2;
	public nodeTree()
	{
		node1 = new node(1);
		node2 = new node(2);
		node2.parent = node1;
	}
	
	public void test()
	{
	    if(node1 ==node2.parent)
	    	System.out.println("node1 = node2.parent!");
	    else System.out.println("node1 !=node2.parent!");
	    
	    if(node1.parent == null)
	    	System.out.println("node1's parent is null");
	    else  System.out.println("node1's parent is "+node1.parent.id);
	    
	    if(node2.parent == null)
	    	System.out.println("node2's parent is null");
	    else  System.out.println("node2's parent is "+node2.parent.id);
	}
}




测试代码:

public class Serial2 
{
	//序列化 nodeTree,node1和node2的引用关系存在
	public void test() throws IOException, ClassNotFoundException
	{
		nodeTree tree = new nodeTree();
		tree.test();
	    
	    FileOutputStream fos = new FileOutputStream("temp1.out");  	     
	    ObjectOutputStream oos = new ObjectOutputStream(fos);  	 
	    oos.writeObject(tree);  	 
	    oos.flush();   
	    oos.close();     
	    
	    FileInputStream fis = new FileInputStream("temp1.out");  	      
	    ObjectInputStream oin = new ObjectInputStream(fis);    
	    tree = (nodeTree) oin.readObject(); 
	    System.out.println("序列化和反序列化之后");
	    tree.test();
	}
	
	//各自序列化node1和node2,之间的引用关系不存在
	public void test2() throws IOException, ClassNotFoundException
	{
		node node1 = new node(1);
		node node2 = new node(2);
		node2.parent = node1;
		
	    System.out.println(node1.id);
	    System.out.println(node2.id);
	    
	    if(node1 ==node2.parent)
	    	System.out.println("node1 = node2.parent!");
	    else System.out.println("node1 !=node2.parent!");
	    
	    if(node1.parent == null)
	    	System.out.println("node1's parent is null");
	    else  System.out.println("node1's parent is "+node1.parent.id);
	    
	    if(node2.parent == null)
	    	System.out.println("node2's parent is null");
	    else  System.out.println("node2's parent is "+node2.parent.id);
	    
	    
	    //序列化
	    FileOutputStream fos = new FileOutputStream("temp1.out");  	     
	    ObjectOutputStream oos = new ObjectOutputStream(fos);  	 
	    oos.writeObject(node1);  	 
	    oos.flush();   
	    oos.close();     
	    
	    fos = new FileOutputStream("temp2.out");  	     
	    oos = new ObjectOutputStream(fos);  	 
	    oos.writeObject(node2);  	 
	    oos.flush();   
	    oos.close(); 
	    
	    //反序列化
	    FileInputStream fis = new FileInputStream("temp1.out");  	      
	    ObjectInputStream oin = new ObjectInputStream(fis);    
	    node1 = (node) oin.readObject(); 
	    
	    
	    fis = new FileInputStream("temp2.out");  	      
	    oin = new ObjectInputStream(fis);    
	    node2 = (node) oin.readObject(); 
	    
	    
	    System.out.println("--------------------------------");
	    System.out.println("序列化和反序列化之后");
	    System.out.println(node1.id);
	    System.out.println(node2.id);
	    
	    if(node1 ==node2.parent)
	    	System.out.println("node1 = node2.parent!");
	    else System.out.println("node1 !=node2.parent!");
	    
	    if(node1.parent == null)
	    	System.out.println("node1's parent is null");
	    else  System.out.println("node1's parent is "+node1.parent.id);
	    
	    if(node2.parent == null)
	    	System.out.println("node2's parent is null");
	    else  System.out.println("node2's parent is "+node2.parent.id);
	    
	}
	
	
	
	public static void main(String[] args) throws ClassNotFoundException, IOException 
	{
		// TODO Auto-generated method stub
		Serial2 test = new Serial2();
		//test.test();
		test.test2();
	}

}



test()的输出结构:

node1 = node2.parent!
node1's parent is null
node2's parent is 1
序列化和反序列化之后
node1 = node2.parent!
node1's parent is null
node2's parent is 1


test2()的输出结果:

1
2
node1 = node2.parent!
node1's parent is null
node2's parent is 1
--------------------------------
序列化和反序列化之后
1
2
node1 !=node2.parent!
node1's parent is null
node2's parent is 1