or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
:::section{.main}
Serialization in java is the process of converting the state of an object into a byte stream. A byte stream is a Java I/O (Input/Output) stream, essentially a flow of data that a programmer can read from or write to. Byte Streams read or write one byte of data at a time.
However, we must convert these byte streams back to their respective Objects to use them again. This reverse process of converting an object into byte-stream is called Deserialization.
Serialization in Java can be implemented using the
java.io.Serializable
interface. ThewriteObject()
method of theObjectOutputStream
class is used for serializing an Object. To make a Java object serializable, we implement thejava.io.Serializable
interface.While Serialization, the
writeObject()
method is used. Meanwhile, in Deserialization, thereadObject()
method is used. Also, for an object to be made serializable, it is mandatory for the object’s class to implement the Serializable interface.The
Serializable
interface tells the JVM (Java Virtual Machine) that the objects of this class are ready for serialization and/or deserialization. The code that we write is called source code. We know that machines do not understand the human language. To bridge the gap, we need to translate the source code. This is where the JVM comes in. It converts source code to something called bytecode, which is then translated to the machine language.Advantages of Serialization
Important Points
During deserialization, the object's constructor isn't invoked.
:::
:::section{.main}
Examples of Serialization and Deserialization in Java
Let’s look at one example to show how serialization in Java works programmatically. Here, we are creating a
Student
class and serializing the object of theStudent
class.Explanation:
Object
of typeStudent
. A text file calledstoreObject
is created with the help of theFileOutputStream
class.FileOutputStream
is an output stream that is used to write data into a file. Next, theObjectOutputStream
class is used to write the object as anOutputStream
.writeObject(Object ob)
is used to serialize objects into byte-streams.Next, let’s look at the code for deserializing the same object. We will use the
readObject()
method of theObjectInputStream
class to deserialise the object.Output:
Explanation:
storeObject.txt
file is accessed with the help of theFileInputStream
, which is used to read data from a file.ObjectInputStream
, which converts the objects from the byte stream that was written usingObjectOutputStream
.readObject()
method is used to deserialize objects.Hence, the process from object to byte stream is complete.
:::
:::section{.main}
Java Serialization with Inheritance (IS-A Relationship)
When a parent class implements the Serializable interface, the child classes do not have to do so. This is a case of Serialization with Inheritance. Inheritance is when a child class extends a parent class and inherits its properties. Hence, the names - parent and child.
Example:
Since
Student
extendsChild
, which extends the Serializable interface, there is no need for theStudent
class itself to extend this interface.:::
:::section{.main}
Java Serialization with Aggregation (HAS-A Relationship)
If a class Student has an object of type Child and if class Student were to implement the Serializable interface and not B, Java would throw a
NotSerializableException
. Let’s see in the code:Example:
The above code snippet would throw a
NotSerializableException
when a classStudent
object is serialized. ClassStudent
is said to have a relationship with class Child. This is called Aggregation (HAS-A Relationship).:::
:::section{.main}
Java Serialization with the Static Data Member
Static data members are volatile and can change during the serialization to deserialization process. Static means the data member is the only copy of the class, and any change made to it will be felt throughout the program. In other words, it belongs to the whole class.
Consider the below code where x is a static variable:
Example:
Output:
Explanation:
:::
:::section{.main}
Java Serialization with Array or Collection
When dealing with arrays or collections, all contained objects must be serializable. If any object within them lacks serializability, the serialization process will encounter failure.
:::
:::section{.main}
SerialVersionUID
The
serialVersionUID
is a constant. So, while the whole object to byte-stream to object takes place, we need to be sure that the conversion from byte-stream to object is correct.serialVersionUID
attribute to remember versions of aSerializable
class and verify that a class and the serialized object are compatible.serialVersionUID
is optional. If the programmer does not define this constant, Java does it for us.The serialization at runtime associates with each serializable class a
serialVersionUID
, which is used during deserialization to verify that the object that was converted to byte-stream is the one that is being retrieved.What if we do not want to save, i.e. serialize, the state of some data member of the class that gets serialized?
In that case, we use a reserved keyword with the data member(s) that we do not want to serialize, like below:
Take a look at the example below:
Output:
Explanation:
The last print statement will return null as
stu_Addr
is a transient variable, and as such, it will not get serialized and will lose its value.:::
:::section{.main}
Java Transient vs Final
Regarding serialization, final variables are serialized directly by their values. Therefore, marking a final variable as transient serves no purpose, as the compiler assigns the value to the final variable.
In Java, marking a data member as transient means it won't be serialized when the object is written to a file or transferred over a network.
:::
:::section{.summary}
Conclusion
:::