Serialization and deserialization of Java for interview records

question:

1. Do you know serialization and deserialization? how to achieve

2. Why use serialization

Introduction to Serialization

        Serialization is the process of converting the state of an object into a storable or transportable form. During serialization, objects are encoded into a specific format that can be stored in a disk file, sent over a network to a remote system, or stored in an in-memory cache. Conversely, the process of restoring encoded objects from these storage areas to objects is called deserialization. Serialization is an important technology in computer science, often used in distributed applications, network communication, caching mechanisms, persistence, etc.

JSON is also a type of serialization

Implementation of serialization:

        There are two main ways to implement serialization in Java: Java's own serialization and serialization provided by third-party libraries.

  1. Serialization that comes with Java: Java provides the Serializable interface to implement serialization. Just let the object class implement the Serializable interface, the object can be serialized into a byte stream, and can be transmitted on the network or stored in a file.

  2. Serialization provided by third-party libraries: Commonly used third-party libraries include Google's Protobuf, Apache's Avro, and Facebook's Thrift. These libraries use different serialization algorithms to serialize objects, which can provide better performance and smaller serialized data volume.

        Different serialization methods are suitable for different scenarios. Java's built-in serialization method is easy to use, but the serialized data is large, and its performance is not as good as the serialization method provided by a third-party library. The serialization method provided by the third-party library can choose different algorithms according to actual needs to achieve better performance and data compression effect.

Why use serialization:

        We know that serialization is used for transmission, who will serialize it to another class in their own class. Therefore, from the perspective of using serialized transmission to answer

  1. network transmission

    When an object needs to be transmitted over the network, the object needs to be serialized into a binary format, which can be conveniently transmitted over the network. After the receiver receives the object, the binary data can be converted back into an object by deserialization.
  2. Persistence

    When objects need to be persisted to disk or database, objects can be serialized into binary format and then stored in files or databases. When the object needs to be read, the binary data can be converted back into an object by deserialization.
  3. cross language transfer

    When objects need to be transferred between different programming languages, the objects can be serialized into a standard format (such as JSON or XML), and the receiver converts the data into its own object according to the standard format. In this way, cross-language communication can be easily realized.

        In short, serialization can convert objects into binary format, so that objects can be easily transferred and persisted in different systems. At the same time, the serialization mechanism in Java is a general mechanism that can support cross-language transmission

Why do you need to serialize in Java when you have JSON?

        Normally, we can use JSON format for serialization and deserialization of objects. But in some cases it might be more appropriate to use Java serialization:

  1. data integrity

    Java serialization is the complete serialization and deserialization of objects, including the internal state and all attributes of objects, which can ensure data integrity and consistency.
  2. object type flexibility

    Using Java serialization, the type information of the object can be included during the transmission process, and an instance of the object can be created during deserialization to ensure the flexibility of the object type.
  3. Object Relational Integrity

    Java serialization can serialize the relationship between objects, including parent-child relationship, membership relationship, etc., and can ensure the integrity of the relationship.
  4. Application Scenario

    Java serialization is usually used for serialization of objects inside Java programs, while JSON is often used for data exchange and storage in Web applications.

Benefits of using serialization:

        Serialization in Java is the process of converting an object into a stream of bytes so that the object can be transmitted over the network or saved to a file. Serialization has the following benefits:

  1. Network transmission: During network transmission, objects can be serialized into byte streams for transmission, which can reduce the amount of data transmitted over the network and improve transmission efficiency.
  2. Cross-platform compatibility: Serialized objects can be deserialized in Java virtual machines of different platforms to realize cross-platform data transmission.
  3. Persistent storage: Objects can be serialized and saved to files so that they can be re-read when needed to achieve persistent data storage.
  4. Caching: When objects need to be read frequently, serialized objects can be cached in memory for fast reading and improved system performance.

In general, Java serialization can conveniently implement operations such as data transmission, storage, and caching, and improve system efficiency and performance.

Summarize:

  • JSON is also a type of serialization
  • Serialization can achieve performance and data compression effects
  • Java serialized objects can be deserialized in Java virtual machines on different platforms
  • Java serialization is the complete serialization and deserialization of objects, including the internal state of the object, all properties, parents, etc., to ensure data integrity and consistency.

Guess you like

Origin blog.csdn.net/qq_37761711/article/details/130298436