Enrol to start learning
Reading is open to everyone. Enrolling is free, and it is what unlocks the audio lessons, practice tests and progress tracking.
20.4. Deserialization
Learn content
Interactive Audio Lesson
Unlock the classroom podcast
The transcript is free to read. A free account plays the conversation back.
Today, we're diving into deserialization, which allows us to reconstruct an object from its serialized state. Can anyone tell me what serialization is?
It's the process of converting an object into a byte stream.
Exactly! And deserialization reverses that. Why do you think this process is important?
It's important for recovering data, right? Like when we save information to a file.
Yes! This capability allows applications to retain user data, maintain states, and facilitate communication in distributed systems. Just think of it as unwrapping a gift. You uncover the original object from its packaging—or in computer terms, from the byte stream.
So, does that mean we can send objects across networks and then reconstruct them?
Exactly! When you serialize an object to send it over a network, deserialization lets the receiver reconstruct the original object. It's crucial for remote method invocation and similar scenarios.
Can we see a practical code example of this?
Great question! Let's look at the DeserializeDemo example I provided. It reads the student.ser file and reconstructs the Student object.
The main takeaway is that deserialization is essential for restoring state and sharing object data across different systems.
Unlock the classroom podcast
The transcript is free to read. A free account plays the conversation back.
Let’s delve into the DeserializeDemo class to illustrate how we can actually perform deserialization. Who can describe the first steps in our code?
We need to create a FileInputStream object to read from the student.ser file.
Correct! Then what comes next after obtaining the input stream?
We create an ObjectInputStream to read the object from the byte stream.
Exactly! Now, once we have our ObjectInputStream, we call readObject(). What does this do?
It reconstructs the Student object from the byte stream.
That's right! After reading it, we can access the object's data, like the student's ID and name. Does anyone recall what will happen if we try to deserialize an object that is not serializable?
We'll get a NotSerializableException.
Well done! Knowing how to properly manage serialization and deserialization helps you handle object states efficiently. We have successfully covered the deserialization concept with an example. Can someone summarize the key steps we took?
We opened a file stream, created an object input stream, read the object, and then accessed its fields.
Perfect summary! This is the cycle of deserialization.
Overview
Short Summary
Deserialization is the process of reconstructing an object from its serialized byte stream, allowing for the retrieval of data that was previously saved or transferred.
Medium Summary
In this section, we explore deserialization, which is the inverse operation of serialization. By converting a byte stream back into an object, developers can restore data states, making deserialization crucial for applications that involve data persistence and communication between different system components.
Detailed Summary
Deserialization in Java
Deserialization is the mechanism in Java that enables the reconstruction of an object from its serialized byte stream, enabling the retrieval of the original object state. It is an essential part of data persistence and remote communication in applications. In this section, we will cover:
-
Concept of Deserialization: This operation involves reading the byte stream generated during serialization and reconstructing the object’s state. It is particularly useful for retrieving objects that were previously saved or transmitted over a network.
-
Example: The provided example features a
DeserializeDemoclass that demonstrates how to read a serializedStudentobject from a file namedstudent.ser. By utilizingFileInputStreamandObjectInputStream, the deserialization process is executed with straightforward methods, resulting in the output of theidandnameof the deserializedStudentobject. -
Significance: Understanding deserialization is critical for managing object states across different sessions and maintaining the integrity of distributed systems. This process facilitates the restoration of objects in various applications, enhancing the ability to save program states efficiently.
Reference YouTube Videos
Audio Book
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free accountDeserialization is the process of reconstructing the object from its serialized byte stream.
Detailed Explanation
Deserialization is the procedure where data, which has been serialized into a byte stream, is converted back into its original object form. This process is crucial for the retrieval of data that was previously saved for future use, allowing programs to read the complete state of objects from binary data.
Examples & Analogies
Imagine you are unpacking a box of toys you had stored away. When you packed the toys, you put them in a specific order and arrangement (serialization). Now, when you're unpacking them, you want to return them to their original state to play with them again (deserialization). It's about bringing back the original form from the stored state.
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free accountExample
import java.io.*;
public class DeserializeDemo {
public static void main(String[] args) throws Exception {
FileInputStream fin = new FileInputStream("student.ser");
ObjectInputStream in = new ObjectInputStream(fin);
Student s = (Student) in.readObject();
in.close();
fin.close();
System.out.println("Deserialized Student: " + s.id + ", " + s.name);
}
}Detailed Explanation
This Java example shows how deserialization works. The program reads from the serialized file student.ser, where a Student object was previously stored. It uses ObjectInputStream to create a stream for reading objects. The readObject() method retrieves the serialized Student object, which is then cast back to the Student type. After closing the streams, it prints the id and name of the deserialized Student. This illustrates the actual code implementation of deserialization in Java.
Examples & Analogies
Think of the serialized file as a movie that captures the life of a student. When you run this code, it's like playing the movie again, allowing you to see the highlights (the student’s ID and name) we recorded before when the movie was put on pause.
--
Key concepts
Core takeaways and short definitions to help you quickly recall the key ideas from this section.
- Deserialization:
The process of reconstructing an object from its serialized state.
- ObjectInputStream:
A Java class used to read serialized objects from a byte stream.
- Serializable interface:
An interface that classes must implement to allow serialization.
Examples
Step-by-step examples to apply the section's ideas and test your understanding.
The 'DeserializeDemo' class demonstrates how to read a serialized 'Student' object from 'student.ser', reconstructing it to access its properties.
If a non-serializable object is encountered during deserialization, a 'NotSerializableException' is thrown.
Memory aids
When you serialize, you send it far, / In a byte stream, like a shooting star. / When you deserialize, it comes back dear, / Bringing back the object, that’s very clear.
Imagine you have a box (the byte stream) that holds a toy (the object). When you send the toy to a friend, you serialize it in the box. Your friend later opens the box and finds the toy just as you left it. This is deserialization.
To remember the steps: 'Read & Restore' - you read from the byte stream and restore the object state.