• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Difference between Serializable and Externalizable

May 19, 2017 By j2eereference Leave a Comment

1)Serializable is a marker interface which doesn’t have any method where as Externalizable extends Serializable interface and it provides method : writeExternal() and readExternal()

2)In Serializable , we only need to implement serializable interface which in turn provides its own default serialization process.
In Externalizable,we require to override writeExternal() and readExternal() methods to start serialization process.

3)In Serializable interface constructor is not called during deserialization process where as constructor is called during deserialization process in externalizable interface.

4)We can customize default serialization process of Serializable interface by defining methods readObject() and writeObject() of ObjectInputStream Class whereas in Externalizable interface ,serialization process is completely customized as we require to override method writeExternal() and readExternal().

5)Serializable interface provides less control over serialization because it is not mandate to use readObject() and writeObject() method as Serializable is a marker interface which provides default serialization process .On other hand Externalizable provides full control on serialization process as it is mandatory to override writeExternal() and readExternal() methods.

6)Externalizable interface gives better performance as using default serialization process of serializable interface we cant do much except making some non-required fields as transient and static but with Externalizable interface we can have full control over Serialization process.

7)In Serializable interface , it is very tough to modify class structure as any modification may break the serialization where as in Externalizable it is easy to modify class structure as developer has complete control over serialization logic.

Default Constructor is not getting called during Deserialization process if we implement Serializable Interface:

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
 
class StudentInformation implements  Serializable
{
  private static final long serialVersionUID = 1L;
  private Integer rollNo;
  private String name;
  public StudentInformation()
  {
    System.out.println("No argument constructor is called");
  }
  public StudentInformation(Integer rollNo,String name)
  {
    System.out.println("Parameterised constructor is called");
    this.rollNo = rollNo;
    this.name = name;
  }
  @Override
  public String toString()
  {
    return "Student Information is rollNo : " + rollNo + "and Student name is : "+name ;
  }
}
 
public class SerializableDemo
{
   public static void main(String[] args) {
   StudentInformation object1 = new StudentInformation(21,"Shobhna");
   try {
         OutputStream fout = new FileOutputStream("ser.txt");
         ObjectOutput oout = new ObjectOutputStream(fout);
         System.out.println("Serializing StudentInformation object");
         oout.writeObject(object1);
         fout.close();
         oout.close();
         System.out.println("Serialization process completed.");
 
         InputStream inputStream=new FileInputStream("ser.txt");
         ObjectInput outInput=new ObjectInputStream(inputStream);
         System.out.println("");
         System.out.println("DeSerializing StudentInformation objects");
         StudentInformation std=(StudentInformation)outInput.readObject();
         System.out.println(std);
         inputStream.close();
         outInput.close();
         System.out.println("Object DeSerialization completed.");
      } catch (IOException | ClassNotFoundException  e) {
        e.printStackTrace();
      }
   }
}

OutPut :
Parameterised constructor is called
Serializing StudentInformation object
Serialization process completed.

Deserializing StudentInformation objects
Student Information is rollNo : 21and Student name is : Shobhna
Object Deserialization completed.

Default Constructor is getting called during Deserialization process if we implement Externalizable Interface:

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
 
class StudentDetail implements Externalizable 
{
   private static final long serialVersionUID = 1L;
   private Integer rollNo;
   private String name;
   public StudentDetail()
   {
     System.out.println("No argument constructor is called");
   }
   public StudentDetail(Integer rollNo,String name)
   {
     System.out.println("Parameterised constructor is called");
     this.rollNo = rollNo;
     this.name = name;
   }
  @Override
  public String toString()
  {
    return "Student Information is rollNo : " + rollNo + "and Student name is : "+name ;
  }
 
 @Override
public void writeExternal(ObjectOutput oo) throws IOException
{
   oo.writeInt(rollNo);
   oo.writeObject(name);
}
 
public void readExternal(ObjectInput in) throws IOException,  ClassNotFoundException
{
   this.rollNo=in.readInt();
   this.name=(String) in.readObject();
}
 
}
public class ExternalizableDemo
{
  public static void main(String[] args) {
  StudentDetail studentDetail = new StudentDetail(21,"Shobhna");
  try {
        OutputStream fout = new FileOutputStream("ser.txt");
        ObjectOutput oout = new ObjectOutputStream(fout);
        System.out.println("Deserializating StudentDetail object");
        oout.writeObject(studentDetail);
        fout.close();
        oout.close();
        System.out.println("Serialization completed.");
       //DeSerialization process >
 
       InputStream fin=new FileInputStream("ser.txt");
       ObjectInput oin=new ObjectInputStream(fin);
       System.out.println("\nDeSerializating StudentDetail object");
       StudentDetail stDetail=(StudentDetail)oin.readObject();
       System.out.println(stDetail);
       fin.close();
       oin.close();
       System.out.println("DeSerialization completed.");
     } catch (IOException | ClassNotFoundException  e)
     {
       e.printStackTrace();
     }
  }
}

OutPut:

Parameterised constructor is called
Deserializating StudentDetail object
Serialization completed.

DeSerializating StudentDetail object
No argument constructor is called
Student Information is rollNo : 21and Student name is : Shobhna
DeSerialization completed.

Please have a look at the posts on Serializable and Externalizable.

 

 

Related Posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Difference between Stack and Heap memory
  • What is ThreadMXBean in java?
  • What is serialVersionUID
  • What is exchanger in concurrency?

Filed Under: Core Java Tagged With: Difference between Serializable and Externalizable, Externalizable, Externalizable program, Serializale

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.