Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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.
Let’s begin!
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.
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.
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.
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:
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.
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:
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.
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 12th October,2024 12th October SAT&SUN (Weekend Batch) | View Details |
Java Course Online | Class Starts on 14th December,2024 14th December SAT&SUN (Weekend Batch) | View Details |
edureka.co