Java Serialization Format

Serialization is the conversion of the state of an object into a byte stream; deserialization does the opposite. Stated differently, serialization is the conversion of a Java object into a static stream (sequence) of bytes which can then be saved to a database or transferred over a network. Java - Serialization 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. 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.

Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.

The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform.

To make a Java object serializable we implement the java.io.Serializable interface.
The ObjectOutputStream class contains writeObject() method for serializing an Object.

The ObjectInputStream class contains readObject() method for deserializing an object.

Java

Advantages of Serialization
1. To save/persist state of an object.
2. To travel an object across a network.

Only the objects of those classes can be serialized which are implementing java.io.Serializable interface.
Serializable is a marker interface (has no data member and method). It is used to “mark” java classes so that objects of these classes may get certain capability. Other examples of marker interfaces are:- Cloneable and Remote.

Points to remember
1. If a parent class has implemented Serializable interface then child class doesn’t need to implement it but vice-versa is not true.
2. Only non-static data members are saved via Serialization process.
3. Static data members and transient data members are not saved via Serialization process.So, if you don’t want to save value of a non-static data member then make it transient.
4. Constructor of object is never called when an object is deserialized.
5. Associated objects must be implementing Serializable interface.
Example :

SerialVersionUID
The Serialization runtime associates a version number with each Serializable class called a SerialVersionUID, which is used during Deserialization to verify that sender and reciever of a serialized object have loaded classes for that object which are compatible with respect to serialization. If the reciever has loaded a class for the object that has different UID than that of corresponding sender’s class, the Deserialization will result in an InvalidClassException. A Serializable class can declare its own UID explicitly by declaring a field name.
It must be static, final and of type long.
i.e- ANY-ACCESS-MODIFIER static final long serialVersionUID=42L;

If a serializable class doesn’t explicitly declare a serialVersionUID, then the serialization runtime will calculate a default one for that class based on various aspects of class, as described in Java Object Serialization Specification. However it is strongly recommended that all serializable classes explicitly declare serialVersionUID value, since its computation is highly sensitive to class details that may vary depending on compiler implementations, any change in class or using different id may affect the serialized data.

It is also recommended to use private modifier for UID since it is not useful as inherited member.

serialver
The serialver is a tool that comes with JDK. It is used to get serialVersionUID number for Java classes.
You can run the following command to get serialVersionUID

serialver [-classpath classpath] [-show] [classname…]

Example 1:

// Java code for serialization and deserialization
importjava.io.*;
classDemo implementsjava.io.Serializable
publicinta;
publicDemo(inta, String b)
this.a = a;
}
}
classTest
publicstaticvoidmain(String[] args)
Demo object = newDemo(1, 'geeksforgeeks');
try
//Saving of object in a file
FileOutputStream file = newFileOutputStream(filename);
ObjectOutputStream out = newObjectOutputStream(file);
// Method for serialization of object
file.close();
System.out.println('Object has been serialized');
}
catch(IOException ex)
System.out.println('IOException is caught');
Demo object1 = null;
// Deserialization
{
FileInputStream file = newFileInputStream(filename);
ObjectInputStream in = newObjectInputStream(file);
// Method for deserialization of object
file.close();
System.out.println('Object has been deserialized ');
System.out.println('b = '+ object1.b);
{
}
catch(ClassNotFoundException ex)
System.out.println('ClassNotFoundException is caught');
}

Output :

Example 2:

// Java code for serialization and deserialization
importjava.io.*;
classEmp implementsSerializable {
129348938L;
staticintb;
intage;
// Default constructor
{
this.age = age;
this.b = b;
publicstaticvoidprintdata(Emp object1)
System.out.println('age = '+ object1.age);
System.out.println('b = '+ object1.b);
{
String filename = 'shubham.txt';
// Serialization
FileOutputStream file = newFileOutputStream
ObjectOutputStream out = newObjectOutputStream
out.writeObject(object);
out.close();
+ 'Data before Deserialization.');
object.b = 2000;
System.out.println('IOException is caught');
try{
// Reading the object from a file
(filename);
(file);
// Method for deserialization of object
file.close();
System.out.println('Object has been deserializedn'
printdata(object);
// System.out.println('z = ' + object1.z);
System.out.println('IOException is caught');
System.out.println('ClassNotFoundException'+
}
}

Output:

This article is contributed by Mehak Narang and Shubham Juneja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Java Serialization Format


Recommended Posts:

Java Serialization Formatter