What is serialization
Our objects don’t just live in memory, they need to be transported over the network, or stored for later loading, so we need Java serialization technology.
Java serialization technology is to transform an object into an array composed of binary bytes. The binary data can be saved to disk or transmission network, and the disk or network receiver can deserialize the object of the class in the template of the object’s generic class to achieve the purpose of object persistence.
How do I serialize an object?
To serialize an object, the class of the object must implement the Java serialization interface: java.io.Serializable.
Add a serialization interface to the class
import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = -8475669200846811112L; private String username; private String address; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", address='" + address + '\'' + '}'; }}Copy the code
Serialization/deserialization
You can use classes in the Commons-Lang3 toolkit to serialize and deserialize objects; you don’t have to write them yourself.
import org.apache.commons.lang3.SerializationUtils; public class Test { public static void main(String[] args) { User user = new User(); user.setUsername("Java"); user.setAddress("China"); byte[] bytes = SerializationUtils.serialize(user); User u = SerializationUtils.deserialize(bytes); System.out.println(u); }}Copy the code
Output:
User{username='Java', address='China'}
Copy the code
The above example serializes the object bytes to memory and then deserializes them. Of course, there is also a method to serialize the disk and then deserialize it. The principle is the same, but the destination is different.
Serialization Considerations
- Serialized objects must implement the serialization interface.
- The serialization interface is also implemented for serialized objects whose properties are objects.
- After the object of the class is serialized, the serialization ID of the class cannot be easily changed, otherwise deserialization will fail.
- After the object of the class is serialized, the addition or deletion of attributes of the class does not affect the serialization, only the value is lost.
- If the parent class is serialized, the subclass inherits the serialization of the parent class without having to add a serialization interface.
- If the parent class is not serialized and the subclass is serialized, the attributes in the subclass will be properly serialized, but the attributes of the parent class will be lost and cannot be serialized.
- Binary byte data serialized in Java can only be deserialized by Java, not by other languages. If you want to interact between the front and back ends or between different languages, you usually need to convert the object into Json/Xml data in common format and then restore the original object.
Recommended reading
Dry goods: 2TB architect four-stage video tutorial
Interview: the most complete Java multithreaded interview questions and answers
Interview: the most comprehensive ali advanced Java interview questions in history
Interview: The most complete Spring interview questions in history
Tutorial: The most complete Spring Boot complete video tutorial
Books: 15 must-read books for advanced Java architects
Tools: Recommended an online creation flow chart, mind mapping software
Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.