Java Serialization is not very efficient. If you serialize bloated objects having a lot of attributes and properties, you do not wish to serialize. This is where Externalization in Java comes into the picture. This article will help you understand the working of Externalization.
- What is Externalization in Java?
- When to use Externalization?
- What is the Externalization interface?
- Difference between Externalization and Serialization
Let’s begin!
What is Externalization in Java?
Externalization in Java is used whenever you need to customize the serialization mechanism. If a class implements an Externalizable interface, then serialization of the object will be done using the method writeExternal(). When an Externalizable object is reconstructed at the receiver’s end, an instance will be created using no-argument constructor and this method is called readExternal().
This basically serves the purpose of custom Serialization, where you can decide what to store in a stream.
When do you use Externalization?
If you want to serialize only part of an object, then Externalization is the best option. You will have to serialize only required fields of an object.
What is the Externalization interface?
If you want to control the process of reading and writing the objects during serialization and de-serialization process, you need to have the object’s class implemented the interface java.io.Externalizable. Only then you can implement your own code to read and write the object’s states. The methods, readExternal() and writeExternal() are defined by the Externalizable interface.
Let’s understand these methods in detail.
readExternal(ObjectInput in)
The object of the interface implements this method which helps in restoring its contents by calling the methods of DataInput for primitive types. It also calls readObject for objects, strings, and arrays. Now let’s discuss how to implement this readExternal method.
As this readExternal() method takes an Object input, you can use its method to read the object’s states from the underlying stream of these rules:
- For primitive types, you can use the readXXX() methods of the DataInput interface. They are namely, readBoolean(), readByte(), readInt(), readLong().
- If you have object types like Strings, arrays, any of your custom classes, you use the readObject() method.
Example:
public void readExternal(ObjectInput in) throws ClassNotFoundException, IOException { this.code = in.readInt(); this.name = (String) in.readObject(); this.password = (String) in.readObject(); this.birthday = (Date) in.readObject(); }
As you can see here, I have de-serialized the following attributes: code, name, password and birthday.
writeExternal(ObjectOutput out)
The object of the interface implements this method in order to save the contents by calling the methods of DataOutput for primitive values or call the writeObject method of ObjectOutput for objects, strings, and arrays. Now, let’s have a look at the implementation process.
As this writeExternal() method takes an ObjectOutput, you can use its method to write object’s states into the underlying stream follow these rules:
- For primitive types, use the writeXXX() methods of the DataOutput interface, like writeBoolean(), writeByte(), writeInt(), writeLong(), etc.
- For object types like Strings, arrays, your custom classes, you can use the writeObject() method.
Example:
public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(code); out.writeObject(name); // write empty password: out.writeObject(""); out.writeObject(birthday); }
However, here, you can see that I have serialized the following attributes: code, name, password and birthday.
Now, moving on to the next topic in this Externalization in Java article, let’s discuss the major differences between Externalization and Serialization in Java.
Externalization vs Serialization: Difference between Externalization and Serialization
This is one of the most frequently asked Java interview questions.
Parameters | Externalization | Serialization |
Process | Uses Custom Serialization process | Uses Default serialization process |
UID | No need of a UID | It needs serialVersionUID |
Storage | You have to store the data which have an object | You can store the object directly |
Access | The externalizable interface provides complete control of the serialization process to application. | No such access |
I hope you guys are clear with Externalization vs Serialization. So with this, we come to the end of this article on “Externalization in Java”. I hope you guys are clear with the topics shared with you.
I hope the above-mentioned content proved to be helpful in enhancing your Java knowledge. Keep reading, keep exploring!
Also check out Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.