Serializable and Externalizable in Java

1. Introduction

What is the difference between Serializable and Externalizable in Java is among the famous Java interview questions. this is one of those difficult Java questions that nobody wants to see in the Java interview.

What makes serialization issues tricky is that serialization as a persistence mechanism is not very popular.

Serializing objects in Java allows you to create a sequence of bytes from any object that has implemented the Serializable interface, it also allows you to transform this sequence of bytes into an object. The mechanism is independent of the operating system, which means that you can transfer objects over your network and restore them on the other side.

2. Serializable

Serializable is a JAVA interface. Thanks to it we can easily serialize any object. This interface does not contain any methods, so it just tells the JVM (Java Virtual Machine) that this class is serializable. All primitive types in JAVA like Integer, Double, String, etc .. supports serialization.

Example of Serializable :

class SerializableExample implements Serializable {
private static final long serialVersionUID = 50999339933L;
String name;
}

Note that serialVersionUID, is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID then deserialization will result in an InvalidClassException.

In serialization, JVM ignores transient variables during serialization and deserialization of Java objects.

class SerializableExample implements Serializable {
private static final long serialVersionUID = 50999339933L;
String name;
transient int age; //the variable will not be serializable
}

Generally, default serialization is easy to implement but has a higher performance cost because it uses reflection which causes relatively slow performance.

3. Externalizable

In some advanced cases, you may want to manipulate the serialization of the object by your self and not pass it to the JVM. For example, you may have some security-sensitive parts of the object, like passwords.

You can control the process of serialization by implementing the Externalizable interface instead of Serializable.

This interface extends the original Serializable interface and adds writeExternal() and readExternal(). These two methods will automatically be called in your object’s serialization and deserialization, allowing you to control the whole process.

Example of Externalizable:

class ExternalizableExample implements Externalizable {
Integer age;
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(age);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
this.age = in.readInt();
}
}

There is another important difference between serialization and externalization is that when we serialize an Externalizable object, a default constructor will be called automatically. only after that, thereadExternal() method will be called.

In this serialization mode, the transient keyword will not restrict any object in the serialization process.

So as we can see serialization using Externalizable interface, add more responsibility to the programmer, but often result in better performance.

Software Engineer. I’m not perfect. still running after my dreams. going to the moon 🌑

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store