“对象序列化”是什么意思?你能举例解释一下吗?


当前回答

序列化是通过将对象转换为字节码来保存对象的特定状态的过程。转换后的字节码用于在2个JVM之间传输对象状态,接收JVM在其中反序列化字节码以检索共享对象的状态。 序列化和反序列化是使用serialVersionUID作为相关jvm中的引用来完成的。

在Java中,使用Serialization和Externalisation接口也可以实现同样的功能

其他回答

我将提供一个类比,以潜在地帮助巩固对象序列化/反序列化的概念目的/实用性。

I imagine object serialization/deserialization in the context of attempting to move an object through a storm drain. The object is essentially "decomposed" or serialized into more modular versions of itself - in this case, a series of bytes - in order to effectively be granted passage through a medium. In a computational sense, we could view the path traveled by the bytes through the storm drain as being akin to bytes traveling through a network. We're transmuting our object in order to conform to a more desirable mode of transportation, or format. The serialized object will typically be stored in a binary file which may later be read from, written to, or both.

也许一旦我们的对象能够作为分解的字节序列通过drain,我们可能希望将对象的表示形式作为二进制数据存储在数据库或硬盘驱动器中。不过,主要的要点是,通过序列化/反序列化,我们可以选择让对象在序列化后保持二进制形式,或者通过执行反序列化来“检索”对象的原始形式。

序列化一个类:将一个对象转换为字节和字节返回到对象(反序列化)。

class NamCls implements Serializable
{
    int NumVar;
    String NamVar;
}

对象序列化是将对象的状态转换为字节蒸汽的过程。

|->当您希望对象在JVM生命周期之外仍然存在时实现。 |->序列化对象可以存储在数据库中。 |->可序列化对象不能被人类读取和理解,因此我们可以实现安全性。

对象-反序列化是获取对象状态并将其存储到对象(java.lang.Object)的过程。

|->在存储它的状态之前,它检查serialVersionUID表单input-file/network和.class文件serialVersionUID是否相同。 如果不抛出java.io.InvalidClassException。

一个Java对象只有在它的类或它的任何超类时才是可序列化的

实现java.io.Serializable接口或 它的子接口java.io.Externalizable。

|=>类中的静态字段不支持序列化。

class NamCls implements Serializable
{
    int NumVar;
    static String NamVar = "I won't be serializable";;
}

如果你不想序列化一个类的变量,使用transient关键字

class NamCls implements Serializable
{
    int NumVar;
    transient String NamVar;
}

如果一个类实现了可序列化,那么它的所有子类也都是可序列化的。

|=>如果一个类有另一个类的引用,所有的引用必须是Serializable,否则序列化过程将不会执行。在这种情况下,NotSerializableException将在运行时抛出。

将文件返回为Object: http://www.tutorialspoint.com/java/java_serialization.htm

        import java.io.*;

        public class SerializeDemo
        {
           public static void main(String [] args)
           {
              Employee e = new Employee();
              e.name = "Reyan Ali";
              e.address = "Phokka Kuan, Ambehta Peer";
              e.SSN = 11122333;
              e.number = 101;

              try
              {
                 FileOutputStream fileOut =
                 new FileOutputStream("/tmp/employee.ser");
                 ObjectOutputStream out = new ObjectOutputStream(fileOut);
                 out.writeObject(e);
                 out.close();
                 fileOut.close();
                 System.out.printf("Serialized data is saved in /tmp/employee.ser");
              }catch(IOException i)
              {
                  i.printStackTrace();
              }
           }
        }

    import java.io.*;
    public class DeserializeDemo
    {
       public static void main(String [] args)
       {
          Employee e = null;
          try
          {
             FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
             ObjectInputStream in = new ObjectInputStream(fileIn);
             e = (Employee) in.readObject();
             in.close();
             fileIn.close();
          }catch(IOException i)
          {
             i.printStackTrace();
             return;
          }catch(ClassNotFoundException c)
          {
             System.out.println("Employee class not found");
             c.printStackTrace();
             return;
          }
          System.out.println("Deserialized Employee...");
          System.out.println("Name: " + e.name);
          System.out.println("Address: " + e.address);
          System.out.println("SSN: " + e.SSN);
          System.out.println("Number: " + e.number);
        }
    }

我喜欢@OscarRyz的礼物方式。虽然在这里我是在继续这个最初由@amitgupta写的连载故事。

即使知道机器人的类结构和序列化的数据,地球的科学家也不能反序列化的数据,可以使机器人工作。

Exception in thread "main" java.io.InvalidClassException:
SerializeMe; local class incompatible: stream classdesc
:

火星的科学家们正在等待全额付款。一旦付款完成,火星的科学家与地球的科学家分享了序列号。地球科学家把它设置成机器人级别,一切都好了。

序列化是将Java对象转换为字节数组,然后再将其转换为保留状态的对象的过程。适用于通过网络发送对象或将内容缓存到磁盘等各种事情。

请阅读这篇简短的文章,它很好地解释了编程过程的一部分,然后转到Serializable javadoc。你也可能对这个相关的问题感兴趣。