Java Serialization

In the context of data storage and transmission, serialization is the process of converting a data structure or object state into a format that can be stored and restored later in the same or another computer environment.

Java provides same mechanism, where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.

As per JAVA technical article : Object serialization is the process of saving an object's state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time. The Java Serialization API provides a standard mechanism for developers to handle object serialization. The API is small and easy to use, provided the classes and methods are understood.

 The Default Mechanism

To persist an object in Java, we must have a persistent object. An object is marked serializable by implementing the interface, which signifies to the underlying API that the object can be flattened into bytes and subsequently inflated in the future.

Let's look at a persistent class we'll use to demonstrate the serialization mechanism:

As you can see, the only thing we had to do differently from creating a normal class is implement the interface on line 40. The completely empty Serializable is only a marker interface -- it simply allows the serialization mechanism to verify that the class is able to be persisted.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...