如果两个对象引用指向同一个可序列化的对象,那么在java中序列化期间会发生什么? [英] What happens during serialization in java, if two object refrences are pointing to the same serializable Object?

查看:232
本文介绍了如果两个对象引用指向同一个可序列化的对象,那么在java中序列化期间会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果两个对象引用指向同一个可序列化的对象,那么在java中序列化期间会发生什么? Serializable Objects是否会保存两次?

例如:

What happens during serialization in java, if two object refrences are pointing to the same serializable Object? Does the Serializable Objects get saved twice ?
for example :

    class King implements java.io.Serializable {
        private String name="Akbar";
    }

    class Kingdom implements java.io.Serializable {
        King goodKing=new King();
        King badKing=goodKing;
    }

    public class TestSerialization {
        public static void serializeObject(String outputFileName,
                                           Object serializableObject) throws IOException {
            FileOutputStream fileStream=new FileOutputStream(outputFileName);
            ObjectOutputStream outStream=new ObjectOutputStream(fileStream);
            outStream.writeObject(serializableObject);
            outStream.close();
        }

        public static void main(String[] args) {
            Kingdom kingdom=new Kingdom();
            try {
                TestSerialization.serializeObject("Kingdom1.out", kingdom);
            }catch(IOException ex) {
                ex.getMessage();
            }
        }
    }

现在,是否只有一个对象状态是为 goodKing badKing 引用保存还是King对象保存两次?

Now, whether only one object state is saved for both goodKing and badKing refrences or the King object get saved twice ?

推荐答案

ObjectOutputStream 说明会发生什么:

The documentation for ObjectOutputStream says what happens:


对象的默认序列化机制会写入对象的类,类签名以及所有非瞬态和非静态字段的值。对其他对象的引用(瞬态或静态字段除外)也会导致这些对象被写入。 使用引用共享机制对单个对象的多个引用进行编码,以便可以将对象图形恢复为与写入原始图像时相同的形状。

(我的重点)

例如,如果您对单个对象有多个引用,重新构建图形时,最终会对该对象的单个重构版本进行多次引用,而不是引用它的多个等效实例。

E.g., if you have multiple references to a single object, when the graph is reconstituted, you end up with multiple references to a single reconstituted version of that object, not references to multiple equivalent instances of it.

当然,如果被序列化的容器实现了一种不同的机制,行为由该机制决定,而不是默认机制。

Of course, if the container being serialized implements a different mechanism, the behavior is dictated by that mechanism, not the default one.

因此,例如,如果我们有 Thing 测试

So for instance, if we have Thing and Test:

Thing.java

import java.io.*;
import java.util.*;

public class Thing implements Serializable {
    private Map<String,String> map1;
    private Map<String,String> map2;

    public Thing() {
        this.map1 = new HashMap();
        this.map2 = this.map1; // Referring to same object
    }

    public void put(String key, String value) {
        this.map1.put(key, value);
    }

    public boolean mapsAreSameObject() {
        return this.map1 == this.map2;
    }
}

Test.java

import java.io.*;

public class Test implements Serializable {

    public static final void main(String[] args) {
        try
        {
            // Create a Thing
            Thing t = new Thing();
            t.put("foo", "bar");

            // Write it out
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("foo"));
            os.writeObject(t);
            os.close();
            os = null;

            // Read it in
            Thing t2;
            ObjectInputStream is = new ObjectInputStream(new FileInputStream("foo"));
            t2 = (Thing)is.readObject();
            is.close();
            is = null;

            // Same underlying map for both properties?
            System.out.println("t2.mapsAreSameObject? " + t2.mapsAreSameObject());
        }
        catch (Exception e)
        {
            System.out.println("Exception: " + e.getMessage());
        }
    }
}

并运行 java Test ,我们得到:

t2.mapsAreSameObject? true

...因为 Thing 的成员, map1 map2 最终指向单个 HashMap 实例。

...because both of Thing's members, map1 and map2 end up pointing to a single HashMap instance.

这篇关于如果两个对象引用指向同一个可序列化的对象,那么在java中序列化期间会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆