Java Serialization Tutorial

Java Serialization Tutorial

As much Java knows how to use objects, perhaps no other language knows. You can assign the properties of one object to another in three ways – Shallow copying, Deep copying and Cloning. You can imagine a simple primitive data type as an object with wrapper classes. You can see a system IP address or server name as an object with URL class. Finally you can treat a simple file name as an object with File class. Now comes serializing an object. Go on read what a serialized object can do.

Java permits to write an object to a file and later to retrieve the object from the file. When an object is written to a file, the data encapsulated with the object also should go and stored in the file. For this, the object should be processed differently and the process involved is known Java as Serialization. Java permits only serialized objects to write to a file or sent across distributed network. Deserialization is nothing but reconstructing another object, when read, which is very similar to the original one with all the encapsulated data together, without any modification.

Let us learn more in question answer format.

1. What is Java Serialization?

If an object is to be written to a file or sent across network, the object should be serialized, else, the JVM throws "NotSerializableException". That is, JVM will not allow writing an object to a file unless it is serialized.

2. Why an object should be written to a file?

I get you the concept with small snippets of code. Observe,

Employee emp = new Employee(); = "S N Rao";
emp.designation = "Java Trainer";
emp.salary = 9999;

The properties of Employee emp are to be stored in a file, say. Why should we store? Storing in a data structure gives temporary storage where the values are available as long as program executes (as they are stored in RAM). But to have a permanent storage, we write the values to hard disk either in a file or in a database. When written to hard disk, the values can be retrieved later at any time.

3. Which I/O stream class is used to store the object in a file?

ObjectOutputStream is used to write an object with method writeObject(Object obj) and to read later ObjectInputStream is used with method readObject().

4. What Serialization does?

Java Serialization preserves the state of the object. That is, the values like name, designation and salary attached to emp object are never changed (or precision lost) after reading from the file later. This technically called as "preserving the state". That is, Java Serialization is used to preserve the state of the Java object.

Now you are fully aware of Java Serialization. It is the time to go on programming part.

3 thoughts on “Java Serialization Tutorial

  1. spandana

    Sir,I Thankyou for providing this material.I would like to learn struts topic so can you provide material on struts at least to my mail please…..!

Leave a Reply

Your email address will not be published. Required fields are marked *