Home  >  Article  >  Java  >  Why should we implement Serializable serialization interface in java?

Why should we implement Serializable serialization interface in java?

王林
王林forward
2019-11-30 17:03:254296browse

Why should we implement Serializable serialization interface in java?

We learned ObjectInputStream and ObjectOutputStream when we were learning JavaSE. At that time, we felt that as long as an object was to be written to a file, it must be implemented. Serializable interface. In fact, there is absolutely no problem in understanding it this way.

1. Introduction to the problem

Why should we implement the serialization interface in Java? Let’s take a look at the picture below:

Why should we implement Serializable serialization interface in java?

In the Java program, a data is written to the disk. If the data is an ordinary string, then there is no problem, all machines can To recognize a string normally, even if it needs to be converted into the corresponding bytes, the computer also knows how to convert the string into the corresponding bytes (code table), but if it is a Java object, it will be troublesome. The disk does not know what you passed. It is a Java object. In other words, the disk does not know what format to convert the Java object into the corresponding bytes.

Related video tutorial sharing: java learning

We know that a Java object is essentially a class bytecode, and the disk does not know how to write this bytecode How to write to the disk, so you need to "identify" it and tell the disk: "I am a Java object, you want to write it to the disk in this way", but "write it to the disk in this way" disk". Therefore, implementing the Serializable interface just identifies "I am a Java object"

If you understand the above text, it will not be difficult to understand the following picture:

Why should we implement Serializable serialization interface in java?

2. Deepen understanding

Java provides an object serialization mechanism. An object can be represented by a byte sequence that contains information such as the object's data, the object's type, and the attributes stored in the object. After the byte sequence is written to the file, it is equivalent to persisting the information of an object in the file.

On the contrary, the byte sequence can also be read back from the file, reconstruct the object, and deserialize it. The object's data, the object's type, and the data information stored in the object can all be used to create objects in memory. Look at the picture to understand serialization:

Why should we implement Serializable serialization interface in java?

Recommended related articles and tutorials: java entry program

The above is the detailed content of Why should we implement Serializable serialization interface in java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete