<-Pre | Home | Next-> |
What do you maean by Object serialization?
To serialize an object means to convert its state to a byte stream so way that the byte stream can be reverted back into a copy of the object.
A Java object is serializable if its class or any of its superclasses implements either the java.io.Serializable interface or its subinterface, java.io.Externalizable.
eg:For example, the java.awt.Button class implements the Serializable interface, so you can serialize a java.awt.Button object and store that serialized state in a file. Later, you can read back the serialized state and deserialize into a java.awt.Button object.
What is Deserialization?
Deserialization is the process of converting the serialized form of an object back into a copy of the object.
How to Serialize an Object?
To serialize an object into a stream perform the following actions:
>Open one of the output streams, for example FileOutputStream
>Chain it with the ObjectOutputStream
>Call the method writeObject() providing the instance of a Serializable object as an argument
>Close the streams
How many methods in the Serializable interface? Which methods of Serializable interface should I implement?
There is no method in the Serializable interface. It’s an empty interface which does not contain any methods. The Serializable interface acts as a marker, telling the object serialization tools that the class is serializable. So we do not implement any methods.
What is the difference between Serializalble and Externalizable interface? How can you control over the serialization process i.e. how can you customize the seralization process?
When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject() two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class’s serialization process. This interface contains two methods namely readExternal and writeExternal. You should implement these methods and write the logic for customizing the serialization process.
How to make a class or a bean serializable? How do I serialize an object to a file?
Or
What interface must an object implement before it can be written to a stream as an object?
An object must implement the Serializable or Externalizable interface before it can be written to a stream as an object. The class whose instances are to be serialized should implement an interface Serializable. Then you pass the instance to the ObjectOutputStream which is connected to a fileoutputstream. This will save the object to a file.
What happens to the object references included in the object?
The serialization mechanism generates an object graph for serialization. Thus it determines whether the included object references are serializable or not. This is a recursive process. Thus when an object is serialized, all the included objects are also serialized alongwith the original object.Common Usage of serialization.
Whenever an object is to be sent over the network or saved in a file, objects are serialized.
What happens to the static fields of a class during serialization?
There are three exceptions in which serialization doesn’t necessarily read and write to the stream. These are:
1. Serialization ignores static fields, because they are not part of any particular state.
2. Base class fields are only handled if the base class itself is serializable.
3. Transient fields.
What one should take care of while serializing the object?
One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.
What are Transient and Volatile Modifiers
A transient variable is a variable that may not be serialized i.e. the value of the variable can’t be written to the stream in a Serializable class. If you don’t want some field to be serialized, you can mark that field transient or static. In such a case when the class is retrieved from the ObjectStream the value of the variable is null.
Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.
What is Serialization and deserialization?
Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects.What is Externalizable?
Externalizable is an interface which contains two methods readExternal and writeExternal. These methods give you a control over the serialization mechanism. Thus if your class implements this interface, you can customize the serialization process by implementing these methods.
Pre->garbage-collection | Next->exception |
*****Please feel free to give feedback at shrawan.iitg@gmail.com and comments below *****
No comments:
Post a Comment