简体   繁体   English

为什么序列化无需实现Serializable就可以工作

[英]Why does serialization work without implementing Serializable

Is it mandatory to implement Serializable class when serializing a class .I tried to put object data into file with and without serializable implementation and found absolutely no difference. 序列化一个类时是否必须实现Serializable类。我尝试将对象数据放入有或没有可序列化实现的文件中,发现绝对没有区别。

snippet -1 片段-1

import java.io.*;
import java.net.*;
public class SerializableTest {

    int a= 10;
    String test="Serialize test";

    public static void main(String [] args){

        SerializableTest test =new SerializableTest();
        test.save();
    }

    public void save(){

        try{

            FileOutputStream fs = new FileOutputStream("save.res");
            ObjectOutputStream os = new ObjectOutputStream(fs);
            os.writeObject(test);
            os.close();

        }
        catch (Exception ex ){
            System.out.println("Error in opening or saving file");

        }
        System.out.println("Complete");
    }
}

snippet -2 片段-2

    import java.io.*;
    import java.net.*;
    public class SerializableTest **implements Serializable**{

    int a= 10;
    String test="Serialize test";

    public static void main(String [] args){

        SerializableTest test =new SerializableTest();
        test.save();
    }

    public void save(){

        try{

            FileOutputStream fs = new FileOutputStream("save.res");
            ObjectOutputStream os = new ObjectOutputStream(fs);
            os.writeObject(test);
            os.close();

        }
        catch (Exception ex ){
            System.out.println("Error in opening or saving file");

        }
        System.out.println("Complete");
    }
}

In both cases program ran fine and contents of save.res looked absolutely similar. 在这两种情况下,程序都可以正常运行,并且save.res的内容看上去绝对相似。

  1. What is the advantage of implementing Serializable when I can do things without implementing. 当我可以执行某些事情而无需实现时,实现Serializable有什么好处。

Serializable should ideally be implemented by the object that needs to be serialized. 理想情况下,可Serializable应该由需要序列化的对象实现。 String implements Serializable . String实现Serializable You are serializing a String object. 您正在序列化一个String对象。 You are not serializing SerializableTest . 您没有序列化SerializableTest

In other words, it does not matter if SerializableTest implements Serializable or not since SerializableTest is not the object that is being serialized. 换句话说, SerializableTest实现Serializable并不重要,因为SerializableTest不是要序列化的对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此序列化逻辑因“声明了不匹配的可序列化字段”而失败? - Why does this serialization logic fail with 'unmatched serializable field(s) declared'? kryo序列化对非序列化类和类的工作是否具有非可序列化属性? - Does kryo serialization work on non serializable class and class have non serializable attributes? 如果序列化是在层次结构中继承的,则将调用未实现可序列化的类的构造函数。 为什么会这样呢? - If the serialization is inherited in a hierarchy then the constructor of class not implementing serializable will be called. Why is this so? 为什么 Jackson 多态序列化在列表中不起作用? - Why does Jackson polymorphic serialization not work in lists? Serializable如何工作? - How does Serializable work? 为什么实现Serializable的对象的序列化会引发异常? - Why serialization of object that implements Serializable throws exception? 为什么序列化过程需要访问第一个不可序列化的超类的无参数构造函数? - Why does Serialization process require access to a no argument constructor of the first non serializable super class? 除了实现Serializable&Externalizable,还有其他方法可以进行序列化吗? - Is there another way to do serialization apart from implementing Serializable & Externalizable? Java对象序列化而不实现可序列化接口 - Java object serialized without implementing serializable interface 无需实现Serializable接口即可对字段进行序列化 - Field getting serialized without implementing Serializable interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM