What is object serialization?

前端 未结 14 2315
长发绾君心
长发绾君心 2020-11-21 23:23

What is meant by \"object serialization\"? Can you please explain it with some examples?

14条回答
  •  滥情空心
    2020-11-22 00:22

    Daring to answer the 6-year-old question, adding just a very high-level understanding for people new to Java

    What is Serialization?

    Converting an object to bytes

    What is Deserialization?

    Converting bytes back to an object (Deserialization).

    When is serialization used?

    When we want to Persist the Object. When we want the object to exist beyond the lifetime of the JVM.

    Real World Example:

    ATM: When the account holder tries to withdraw money from the server through ATM, the account holder information like withdrawal details will be serialized and sent to the server where the details are deserialized and used to perform operations.

    How serialization is performed in java.

    1. Implement java.io.Serializable interface (marker interface so no method to implement).

    2. Persist the object: Use java.io.ObjectOutputStream class, a filter stream which is a wrapper around a lower-level byte stream (to write Object to file systems or transfer a flattened object across a network wire and rebuilt on the other side).

      • writeObject(<>) - to write an object
      • readObject() - to read an serialized Object

    Remember:

    When you serialize an object, only the object's state will be saved, not the object's class file or methods.

    When you serialized a 2-byte object, you see 51 bytes serialized file.

    Steps how the object is serialized and de-serialized.

    Answer for: How did it convert to 51 bytes file?

    • First writes the serialization stream magic data (STREAM_MAGIC= "AC ED" and STREAM_VERSION=version of the JVM).
    • Then it writes out the metadata of the class associated with an instance (length of the class, the name of the class, serialVersionUID).
    • Then it recursively writes out the metadata of the superclass until it finds java.lang.Object.
    • Then starts with the actual data associated with the instance.
    • Finally writes the data of objects associated with the instance starting from metadata to the actual content.

    If you are interested in more in-depth information about Java Serialization please check this link.

    Edit : One more good link to read.

    This will answer a few frequent questions:

    1. How not to serialize any field in class.
      Ans: use transient keyword

    2. When child class is serialized does parent class get serialized?
      Ans: No, If a parent is not extending the Serializable interface parents field don't get serialized.

    3. When a parent is serialized does child class get serialized?
      Ans: Yes, by default child class also gets serialized.

    4. How to avoid child class from getting serialized?
      Ans: a. Override writeObject and readObject method and throw NotSerializableException.

      b. also you can mark all fields transient in child class.

    5. Some system-level classes such as Thread, OutputStream, and its subclasses, and Socket are not serializable.

提交回复
热议问题