Serialization is the process of converting Java objects into a stream of bytes. The stream of bytes can be transmitted through a network connection, stored in a database as a BLOB object or saved as a binary file. The stored or transmitted stream of bytes can be reconstructed to Java object later. This article explains Serialization in Java, using a simple step by step example. Show
Create a Student class with the attributes name, and age.
Create a Main class as shown below, to create an object of Student.
As defined earlier, serialization is the process of converting an object to a stream of bytes. Don't be afraid. The term stream of bytes refers just an array of bytes. To serialize the Student object, add a new method toStream to the Main class.
The Main class with toStream method is provided here:
If you run this modified code, you will end up with java.io.NotSerializableException. The reason is: to convert any objects of a class into a stream, a special permission has to be provided for that class. Rather than defining it as a permission. In Java, this permission is provided by implementing the interface java.io.Serializable. This interface does not have any abstract methods. Purpose of this interface is just indicating that, the object of this classes are allowed to convert into a stream of bytes. Even though, there are no methods to override, it is the best practice to add a long variable serialVersionUID to the Serializable class. This id is used to identify the exact class of byte stream, when the stream is converted back to the Java object. The serialVersionUID can be any long number.
Now run the code again and test the output. Fine! we have converted a Student object into an array of bytes. What about converting the byte array back to Student object? Add a new method toStudent in the Main class. This method converts the given array of bytes to a Student object.
Modify the Main class as shown below and test the output.
The above code serializes a Student object to a stream of bytes, and deserailizes it back to the Student object. Let's use serialization to save a Student object to a file and read it back later. Create the save method and call this method from the main method as shown below. Remember to change the file location based on your operating system.
After executing this code, go and check whether the file is created or not. If you do not have any permission issues for the given valid location, the file must be created at the provided location. To read the Student object, add a new method read as shown below and call it from the main method.
This code will read and print the saved student's name Alice, and her age 24. Seems to be good. But what will be the output, if the saved object is read after more than one years? Regardless of number of years, the age will be displayed as 24 forever. A possible trick to overcome this problem is: rather than saving the age, save the birth year to the stream of bytes and redefine the age when deserailize the student back. To ignore the serialization of a property, declare the property as a transient property. Modify the int age as transient int age and run the code.
This time the deserialized student's age will be 0. This is because of the transient modifier. If the age is not available during deserialization, Java will assign the default value of that data type (For int, default value is 0). To save the birth year, add the following method into the Student class.
The writeObject method is called by Java, when the object is serialized to stream of bytes. To restore the age, add the following method into the Student class.
The readObject method is called by Java, when a stream of byte is deserialized to Java object. After the modification, your code should be something similar to this:
This time, output will be updated based on the current year. Conclusively serialization is the process of converting an object into stream of bytes. The converted byte stream can be used for multiple purposes. Java has a marker interface (an interface without any methods) Serializable to flag the classes, that their objects are allowed to undergo the serialization. writeObject and readObject methods are used to perform advanced operations during serialization and deserialization. Find the source codes at Git Hub. What is the process used to convert an object to a stream of bytes that can be saved in a file?Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file.
What is the process used to convert an object to a stream?The process of converting object to stream is called serialization in java.
Is the process in which an object converts into a byte stream?Serialization is the process of converting the state of an object into a byte stream.
How do you convert objects to bytes?Write the contents of the object to the output stream using the writeObject() method of the ObjectOutputStream class. Flush the contents to the stream using the flush() method. Finally, convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.
|