Home  >  Q&A  >  body text

序列化 - 如何理解java.io.Serializable接口

java.io.Serializable是一个没有任何待实现方法的接口,此接口在对象序列化的过程中起到了什么作用?或者此接口在java序列化机制中扮演什么角色?

伊谢尔伦伊谢尔伦2713 days ago433

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 12:05:04

    The Java platform allows us to create reusable Java objects in memory, but generally, these objects may only exist when the JVM is running, that is, the life cycle of these objects will not be longer than the life cycle of the JVM longer. But in real applications, it may be required to save (persist) the specified object after the JVM stops running, and re-read the saved object in the future. Java object serialization can help us achieve this function.
    Excerpted from http://www.blogjava.net/jiangshachina/archive/2012/02/13/369898.html
    PS: The linked article has detailed usage examples

    In plain English, this interface allows you to save the objects you instantiate in memory as files. What’s the use? Just give a few chestnuts
    1. Game archive: Save the protagonist’s strength object as a file. You can load it next time you open the game and continue playing
    2. Network communication: To transfer an instance object on one machine to another machine, first save it as a file and then transfer it


    After changing the subject

    You want to understand the mechanism of serialization, right? Since you asked the question sincerely, Kojiro Musashi~~~~~

    Okay, let’s take a look at the process of persisting an object using the ObjectOutputStream class. The source code of the writeObject0(Object obj, boolean unshared) function is lines 1170~1185 (JDK 1.7.0_45)

     if (obj instanceof String) {
        writeString((String) obj, unshared);
    } else if (cl.isArray()) {
        writeArray(obj, desc, unshared);
    } else if (obj instanceof Enum) {
        writeEnum((Enum) obj, desc, unshared);
    } else if (obj instanceof Serializable) {
        writeOrdinaryObject(obj, desc, unshared);
    } else {
        if (extendedDebugInfo) {
            throw new NotSerializableException(
                cl.getName() + "\n" + debugInfoStack.toString());
        } else {
            throw new NotSerializableException(cl.getName());
        }
    }
    

    See, if the object is a String, an array, an enumeration, or is Serializable, the corresponding function will write the object into a file, otherwise an error will be thrown. What role does it play? It is just a flag.

    If you just let a class implement the Serializable interface without any other processing, the default serialization mechanism is used. Using the default mechanism, when serializing an object, not only the current object itself will be serialized, but other objects referenced by the object will also be serialized. Similarly, other objects referenced by these other objects will also be serialized. analogy. Therefore, if the member variables contained in an object are container class objects, and the elements contained in these containers are also container class objects, then the serialization process will be more complicated and expensive.
    Still excerpted and the previous link

    Externalizable inherits from Serializable. When using this interface, the details of serialization need to be completed by the programmer. I think you need this~~~

    reply
    0
  • 黄舟

    黄舟2017-04-17 12:05:04

    The empty interface is useful when using instanceof to determine the type

    public class Test {
        public static void main(String[] args) {
            T1 t1 = new T1();
            T2 t2 = new T2();
    
            System.out.println("t1: " + (t1 instanceof java.io.Serializable));
            System.out.println("t1: " + (t2 instanceof java.io.Serializable));
        }
    }
    
    class T1 {}
    
    class T2 implements java.io.Serializable {}
    
    

    If you look at the source code of OpenJDK, you can see that there is such a relevant code in ObjectOutputStream.java:

                // remaining cases
                if (obj instanceof String) {
                    writeString((String) obj, unshared);
                } else if (cl.isArray()) {
                    writeArray(obj, desc, unshared);
                } else if (obj instanceof Enum) {
                    writeEnum((Enum) obj, desc, unshared);
                } else if (obj instanceof Serializable) {
                    writeOrdinaryObject(obj, desc, unshared);
                } else {
                    if (extendedDebugInfo) {
                        throw new NotSerializableException(
                            cl.getName() + "\n" + debugInfoStack.toString());
                    } else {
                        throw new NotSerializableException(cl.getName());
                    }
                }
    

    Source code reference http://hg.openjdk.java.net/jdk6/jdk6/jdk/file/d1f592073a0e/src/share/classes/java/io/ObjectOutputStream.java line 1176

    reply
    0
  • Cancelreply