Загрузка...

Java Serialization #coding #programming #javalanguage #javaprogramming #java #faq #serialization

Serialization in Java is the process of converting an object's state into a byte stream, which can be easily stored in a file, transmitted over a network, or stored in a database. The reverse process, converting the byte stream back into a live Java object, is called deserialization.

Core Concepts
Serializable Interface: To make a Java object serializable, its class must implement the java.io.Serializable interface. This is a marker interface and has no methods; its presence simply tells the Java Virtual Machine (JVM) that the class is eligible for serialization.

ObjectOutputStream and ObjectInputStream: These classes are used to perform the actual serialization and deserialization. The writeObject() method serializes an object to an output stream, and the readObject() method reads a byte stream and deserializes it back into an object.

serialVersionUID: A unique identifier for each version of a serialized class. It is highly recommended to declare this explicitly (private static final long serialVersionUID = 1L;) to ensure compatibility between different versions of a class, as the JVM-generated ID is sensitive to class details and can cause InvalidClassException errors if the class changes.

transient Keyword: Fields marked with the transient keyword are ignored during the serialization process and are not saved to the byte stream. They will be reinitialized to their default values (e.g., null for objects, 0 for int) during deserialization. Static fields are also automatically excluded from serialization because they belong to the class, not a specific object instance.

Example
Here is a basic example of how to serialize and deserialize a Person object:
1. Create a Serializable Class:
A class must implement Serializable and can use serialVersionUID for versioning and the transient keyword to exclude fields from serialization, such as a password.

2. Serialization (Writing the object to a file):
Use FileOutputStream and ObjectOutputStream to write a serializable object to a file.

3. Deserialization (Reading the object from a file):
Use FileInputStream and ObjectInputStream to read the object from the file. Fields marked transient will be null or default values after deserialization.

Why use Serialization?
Serialization is useful for persistence, network communication, caching, and deep copy.

Alternatives
Libraries like Jackson or Gson using JSON or XML are often preferred over Java's built-in serialization for better interoperability and security.

Видео Java Serialization #coding #programming #javalanguage #javaprogramming #java #faq #serialization канала Quick Java Tutorial
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять