Javatpoint Logo
Javatpoint Logo

Externalization in Java

Externalization in Java is used to customize the serialization mechanism. Java serialization is not much efficient. When we have bloated objects that hold several attributes and properties, it is not good to serialize them. Here, the externalization will be more efficient.

Let's understand serialization in Java:

What is Serialization in Java

Serialization is a mechanism of writing the state of an object into a byte-stream. The serialization is mainly used in Hibernate, RMI, JPA, EJB and JMS technologies. The mechanism that reverses the process of serialization is called deserialization. These processes are platform-independent means we can serialize objects in one platform and can deserialize them on other platforms.

The Serializable interface is implemented to serialize the objects in Java.

Read more about Serialization and Deserialization in Java.

Now come back to our main topic, externalization in Java.

What is Externalization in Java

Java serialization is not much effective when we have bloated objects with multiple attributes and properties. Here, externalization comes into role. It allows us o customize the serialization. For example, if we have implemented the Serialization interface in a class, we can externalize it using the writeExternal() method. When users reconstruct an externalized object from their end, an instance of the object will be created using the readExternal() method.

Thus, the externalization provides custom serialization, where we can manage our object stream and decide what to store in it.

The externalization is useful if we want to serialize a part of an object. We can serialize only the required fields of an object.

Externalizable Interface

We are required to implement the java.io.Externalizable interface to control reading and writing during the serialization and deserialization. The methods readExternal() and writeExternal() are the parts of Externalizable interface.

Let's understand these methods:

readExternal() method in Externalizable Interface

The Externalizable interfaces' objects are implemented using the readExternal() method. It restores objects by calling the methods of DataInput for primitive types. It can call readObject for objects, strings, and arrays data type.

Let's understand how to implement the readExternal() method.

When we pass an object in the readExternal() method, it takes object input.

For primitive data types, we can use readBoolean(), readByte(), readInt(), readLong() methods.

For String, Arrays, or any of the custom classes, we can use the readObject() method.

It is used as follows:

From the above code snippet, the readInt() and readObject() methods are used to deserialize the values of code, name, password, and birthday.

writeExternal() method in Externalizable Interface

The writeExternal() method of the Externalizable interface is used to save the contents by calling the methods of dataOutput for primitive values. For the objects, strings, and arrays, call the writeObject method of ObjectOutput.

Let's understand the implementation of the writeExternal() method:

The following rules should be followed to write the object's state to the underlying stream:

For the primitive type values, the writeBoolean(), writeByte(), writeInt(), writeLong() methods are used.

For strings, arrays, and custom classes, the writeObject() method is used.

Consider the below code snippet:

From the above code snippet, the writeInt() and writeObject() methods are used to serialize the code, name, password, and birthday values.

Hence, we can see how to customize the serialization using the Externalization in Java.

Example of Externalization in Java

We can save a Java class into a byte stream using externalization. It provides complete control on class persistence as a byte stream. We must implement the Externalizable interface and define its methods, which are writeExternal() and readExternal() in the class, which needs to be externalized.

Let's see an example of Externalization in Java.

User.java:

JTPMain.java:

Output:

After De externalization username: JavaTpoint and age is:25

In the above example, the fields saved by the writeExternal() method will be persisted as a byte stream, and the rest fields will not be externalized. So using the Externalizable interface, we have full control of java class persistence.

Let's discuss the difference between serialization and externalization to clear the picture of both:

Difference between Serialization and Externalization

Key Serialization Externalization
Interface It is a marker interface Whereas, it contains two methods readExternal() and writeExternal()
Process Default serialization Custom serialization
UID serialVersionUID No UID
Storage Stores the data that have objects Directly stores objects
Access No access Complete control to customize the serialization
Performance It provides relatively slow performance. It provides full control over the implementation approach.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA