Java Externalization
What is Externalization in Java?
Externalization is a concept that is advanced to serialization. Serialization is a concept where we can transfer an object from our JVM ( Java virtual machine ) to another JVM through a network. Serialization in Java is not efficient. In serialization, we have no control over how the object is created or over object properties. If we have objects like bloated objects that are objects with too many attributes and a lot of properties serialization is not good practice. Here externalization comes in handy.
Externalizable Interface
In serialization, we have no control over how the object is created or over object properties. This means whatever properties we defined those object properties will be returned to that file, and we can get complete output when we deserialize. Suppose we want to control only one or few properties then we should implement an externalizable interface. An externalizable interface is present in the "java.io" package. Here the object's class should be implemented in the interface " java.io.Externalizable ".
The externalizable interface provides two methods:
- readExternal()
- writeExternal()
readExternal() method in Externalizable interface :
The readExternal() method implements the externalizable interfaces object. By calling the methods of Data Input for primitive types it restores the objects. For objects, strings, and arrays datatypes it can readObjects.
Let’s understand the implementation of the readExternal() method in an externalizable interface :
readExternal() method shows the following rules :
To take object input, we should pass an object in the readExternal() method.
For primitive datatypes, we can use methods like readBoolean(), readInt(), readByte() etc…
We can use the readObject() method for strings, arrays, and any custom classes.
Let‘s understand briefly about the readExternal() method using the program :
public void readExternal(ObjectInput in) throws ClassNotFoundException, IOException {
this.code1 = in.readInt(); // reads integer
this.name1 = (String) in.readObject(); // reads string
this.password1 = (String) in.readObject();
this.birthday1 = (Date) in.readObject();
}
Here, the readInt() method is used to deserialize the code1, and the readObject() method is used to deserialize the name1, password1, and birthday1.
writeExternal() method in Externalizable interface :
First of all, this method is used to save the contents. The contents of primitive datatypes are saved by calling the methods of data outputs. For string, arrays, and for objects writeObject() method is called.
Let us understand briefly.
writeExternal() method shows the following rules :
- writeBoolean(), writeInt(), writeByte(), writeFloat() etc… methods are used for primitive datatypes.
- WriteObject() method is used for arrays, strings, and custom classes.
Let us understand the writeExternal() method by considering the following snippet.
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(code1);
out.writeObject(name1);
out.writeObject("");
out.writeObject(birthday1);
}
From the above program, the writeObject() method is used to serialize the name1, password1, and birthday1.
To serialize the code, we have used writeInt() method.
Let us understand externalization using the program.
Tester.java
import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
public class Tester {
public static void main(String[] args) {
Employee1 e = new Employee1();
e.name1 = "JavaTpoint";
e.age1 = 30;
try (
FileOutputStream fileOut = new FileOutputStream("test.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
) {
out.writeObject(e);
}catch (IOException i) {
System.out.println(i.getMessage());
}
try (
FileInputStream fileIn = new FileInputStream("test.txt");
ObjectInputStream in = new ObjectInputStream(fileIn);
) {
e = (Employee1)in.readObject();
System.out.println(e.name1);
System.out.println(e.age1);
} catch (IOException i) {
System.out.println(i.getMessage());
} catch (ClassNotFoundException e1) {
System.out.println(e1.getMessage());
}
}
}
class Employee1 implements Externalizable {
public Employee1(){}
String name1;
int age1;
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(name1);
out.writeInt(age1);
}
public void readExternal(ObjectInput in) throws ClassNotFoundException,
IOException {
name1 = (String)in.readObject();
age1 = in.readInt();
}
}
Output :

Difference between serialization and externalization
Serialization vs Externalization
Key | Serialization | Externalization |
Interface | It is a marker interface. | Externalizable interface provides two methods they are readExternal() and writeExternal(). |
UID | It needs SerialversionUID. | No UID is used. |
Storage | In serialization, If the data have an object then it can be stored. | In Externalization, we can store the object directly. |
Access | There is no such access. | Complete control of serialization process to application. Is done through externalizable interface. |
Process | This is a default serialization. | This is a custom serialization. |
Performance | It provides relatively slow performance. | For implementation approach, it provides full control. |