
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Serialization and Externalization in Java
Serialization and externalization both are the processes of converting an object to stream byte and storing byte stream in database or memory. The class that implements java.io.Serializable interface can be serialized.
What is Serialization?
Java provides a mechanism called object serialization where an object can be converted into a byte stream that includes the object's data and details about the object's type.
Example
The following is an example of Serialization in Java:
import java.io.Serializable; class SerializableExample implements Serializable { private static final long serialVersionUID = 5081877L; String name; } public class Main { public static void main(String[] args) { SerializableExample obj = new SerializableExample(); obj.name = "Tutorialspoint"; System.out.println("Object name: " + obj.name); } }
The output of the above Java program is:
Object name: Tutorialspoint
What is Externalization?
Externalization is used for custom serialization based on the requirement in the application. If a class implements an Externalizable interface then, object serialization will be done using the writeExternal() method. Externalization extends java.io.Serializable.
Example
The following is an example of Externalization in Java:
import java.io.*; class ExternalizableExample implements Externalizable { Integer id; // Public no-argument constructor is required public ExternalizableExample() { // Optional initialization can be done here if needed } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(id); } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.id = in.readInt(); } } public class Main { public static void main(String[] args) { // Create an instance and assign a value ExternalizableExample example = new ExternalizableExample(); example.id = 123; // Serialization: write the object to a file try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data.ser"))) { out.writeObject(example); System.out.println("Serialized successfully."); } catch (IOException e) { e.printStackTrace(); } // Deserialization: read the object back from the file try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.ser"))) { ExternalizableExample newExample = (ExternalizableExample) in.readObject(); System.out.println("Deserialized ID: " + newExample.id); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } }
The output of the above Java program is:
Serialized successfully. Deserialized ID: 123
Difference between Serialization and Externalization
The following are the differences between Serialization and Externalization:
Sr. No. | Key | Serialization | Externalization |
---|---|---|---|
1 |
Interface |
Serialization is a marker interface |
Externalization contains two methods readExternal and writeExternal. |
2 |
Implementation logic |
The class which is implementing this interface gives the responsibility to JVM for serializing or persist java object. JVM use readObject and writeObject for serialization |
Externalization provides implementation logic control to the application by overriding readExternal and writeExternal methods. |
3 |
Way to ignore variables |
In serialization, JVM ignores transient variables during the serialization and deserialization of a java object |
Programmer can write their own logic to ignore some of the variables during externalization of java object |
4 |
Performance |
A serializable interface uses reflection which causes relatively slow performance. |
Externalizable gives full control over the implementation approach. |
5 |
Object serialization with inheritance |
1. If the superclass is not serializable then the subclass still can be serialized. 2. If a subclass is not serialized but the superclass is automatically serializable |
We can apply this to externalizable as well. |