• Java Tutorial

Serialization is a process of converting an object into a sequence of bytes which can be persisted to a disk or database or can be sent through streams. In this tutorial we will learn about Serialization in core java. Serialization in Java is a mechanism of writing the state of an object into a byte-stream.It is mainly used in Hibernate, RMI, JPA, EJB and JMS technologies. The reverse operation of serialization is called deserialization where byte-stream is converted into an object.

  • Java's serialization algorithm. The algorithm to serialize an object is described as below: 1. It writes out the metadata of the class associated with an instance. It recursively writes out the description of the superclass until it finds java.lang.object.
  • Default java serialization convert the java objects into bytes to send over network. But many times you will need a more cross-platform medium to send this information e.g. XML so that application working on different technologies can also gain advantage of this serialized information.
  • Java Object Oriented
  • Java Advanced
  • Java Useful Resources
  • Selected Reading

Java provides a mechanism, called object serialization 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. Nintendo wii sing it games free.

Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object.

The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out −

The above method serializes an Object and sends it to the output stream. Similarly, the ObjectInputStream class contains the following method for deserializing an object −

This method retrieves the next Object out of the stream and deserializes it. The return value is Object, so you will need to cast it to its appropriate data type.

To demonstrate how serialization works in Java, I am going to use the Employee class that we discussed early on in the book. Suppose that we have the following Employee class, which implements the Serializable interface −

Example

Notice that for a class to be serialized successfully, two conditions must be met −

  • The class must implement the java.io.Serializable interface.

  • All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.

With

If you are curious to know if a Java Standard Class is serializable or not, check the documentation for the class. The test is simple: If the class implements java.io.Serializable, then it is serializable; otherwise, it's not.

Serializing an Object

The ObjectOutputStream class is used to serialize an Object. The following SerializeDemo program instantiates an Employee object and serializes it to a file.

When the program is done executing, a file named employee.ser is created. The program does not generate any output, but study the code and try to determine what the program is doing.

Note − When serializing an object to a file, the standard convention in Java is to give the file a .ser extension.

Example

Deserializing an Object

Serializable Interface Example In Java

The following DeserializeDemo program deserializes the Employee object created in the SerializeDemo program. Study the program and try to determine its output −

Example

This will produce the following result −

Serialization Example In Java Api

Output

Serialization Class Example In Java

Here are following important points to be noted −

Serialize And Deserialize In Java

  • The try/catch block tries to catch a ClassNotFoundException, which is declared by the readObject() method. For a JVM to be able to deserialize an object, it must be able to find the bytecode for the class. If the JVM can't find a class during the deserialization of an object, it throws a ClassNotFoundException.

  • Notice that the return value of readObject() is cast to an Employee reference.

  • The value of the SSN field was 11122333 when the object was serialized, but because the field is transient, this value was not sent to the output stream. The SSN field of the deserialized Employee object is 0.

    Home Security Camera provide a complete free video surveillance solution allowing capture, analysis, recording and monitoring of any CCTV or security cameras. Using off the shelf hardware with any camera, you can design a system as large or as small as you need with our free video surveillance software. Video surveillance software free download. ISpy is the world's most popular open source video surveillance and security software. With more than 2 million users worldwide, iSpy works with more cameras and devices than anything else on.

Coments are closed
Scroll to top