- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to serialize and deserialize an object in Java?
The Serialization is a process of changing the state of an object into a byte stream, an object is said to be serializable if its class or parent classes implement either the Serializable or Externalizable interface and the Deserialization is a process of converting the serialized object back into a copy of an object.
During serialization, if we don’t want to write the state of a particular variable in a byte stream using a transient keyword. When the JVM comes up to the transient keyword, it ignores the original state of a variable and stores a default value of that data type i.e. 0 for int, 0 for byte, 0.0 for float, etc. A Serialization of an object can be done through FileOutputStream and ObjectOutputStream class.
Example
import java.io.*; public class SerializationTest implements Serializable { int a = 1, b = 2; transient int c = 3; public static void main(String[] args) throws Exception { SerializationTest obj = new SerializationTest(); // serialization FileOutputStream fos = new FileOutputStream("serialization.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(obj); // de-serialization FileInputStream fis = new FileInputStream("serialization.txt"); ObjectInputStream ois = new ObjectInputStream(fis); SerializationTest test = (SerializationTest)ois.readObject(); System.out.println("a = " + test.a); System.out.println("b = " + test.b); System.out.println("c = " + test.c); } }
Output
a = 1 b = 2 c = 0
- Related Articles
- ArduinoJSON: Serialize and Deserialize
- Serialize and Deserialize BST in Python
- How to serialize and deserialize a JSON using the ExclusionStrategy interface in Java?
- Serialize and Deserialize Binary Tree in C++
- Serialize and Deserialize N-ary Tree in C++
- How to deserialize a JSON into an existing object in Java?
- How to deserialize a JSON to Java object using the flexjson in Java?
- How to deserialize a Java object from Reader Stream using flexjson in Java?
- How to serialize a JSON object with JsonWriter using Object Model in Java?
- How to serialize and de-serialize generic types using the Gson library in Java?
- How to deserialize a JSON into Javascript object?
- How to serialize a JSON string to an Output Handler in Java?
- How to serialize a lambda function in Java?
- How can we serialize an array of objects using flexjson in Java?
- How to deserialize a JSON string using @JsonCreator annotation in Java?

Advertisements