Serializable in java [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between Serializable and Externalizable in Java?
What is the difference between Serializable and Externalizable interface?

Serializable allows an object to be written out in a standard format; there's some control over part of the process, but mostly it's automatic. Externalizable provides much more complete control (e.g., allowing an object that is a table of numbers to be written out as CSV-format data).
Serializable doesn't require you to write any methods (though it might be a good idea) since it is a marker interface. Externalizable requires the writing of methods.

Serializability of a class is enabled by the class implementing the java.io.Serializable
interface. Classes that do not implement this interface will not have any of their state serialized or deserialized.
Externalizable Only the identity of the class of an Externalizable instance is written in the serialization stream and it is the responsibility of the class to save and restore the contents of its instances.

Related

Why doesn't Serializable interface contain any methods? [duplicate]

This question already has answers here:
Although the Serializable interface in Java has no methods, no fields, it can achieve its function. How?
(6 answers)
Closed 7 years ago.
I know what is serialization and why it is used, but my question:
Why is serialization a marker interface?
What is the potential benefit of not having writeObject, readObject in Serializable interface because when we do serialization we finally override these 2 methods?
How does readResolve ensure that the object created during deserialization is not the new object. I know the below and it is returning the same object during deserialization but who will call this readResolve method internally?
private Object readResolve() throws java.io.ObjectStreamException {
return INSTANCE;
}
Because there needs to be some explicit way of declaring a class to be serializable. The framework can't just assume all classes to be serializable, as there are many kinds of objects that will stop working if their fields are written to disk and are later reloaded from there (e.g. FileInputStream, which relies on an open operating system file handle which might no longer exist when the object is deserialized). The modern way of having such a declaration would be an annotation, but those didn't exist in Java at the time serialization was added.
You won't necessarily need to override them - if the default behavior of the serializer is good enough; you don't need to do anything but implement Serializable.
The serialization framework calls it when an object has been completely deserialized. At this time, the object is allowed to inspect its own contents, and if it decides that it should instead be represented by another instance, it can return that instance instead (if not, it returns this). Whatever comes out of this method is returned to the code that requested the deserialization. If a preexisting object was returned, the new object created by the deserializer will not be seen by anyone and will eventually be garbage collected.
Marker Interfaces are used to tell JVM to perform specific tasks. they don't have any method. Serializable is also a marker interface.
Serialization is the process of flattening the objects. when you implement serializable interface in a class, it tells JVM to serialize its object i.e. it has to be converted into stream.

customization is possible with serialization then why Externalizable at all?

I have read these articles on SO:
Externalizable or Serializable?,
What is the difference between Serializable and Externalizable in Java?.
but my question is what extra customization could be derived by implementing Externalizable as compared to serializable.
It is possible to customize Serialization of a class that implements Serializable by giving our own implementation of writeObject and readObject. Then what is the purpose of using Externalizable and customizing the ReadExternal and writeExternal implementations. what is the real benefit of using Externalizable. I have read various links that says Externalizable supports custom serialization (including the one above). but I do not see an example where Externalizable is a clear winner or something that cannot be done using Serializable. would be nice to see an example on this.
Just to give more clarity, the following snippet, is extracted from here:
When a class implements Serializable interface it gives information to the JVM that the instances of these classes can be serialized. Along with that, there is a special note to the JVM
"look for following two methods in the class that implements Serializable. If found invoke that and continue with serialization process else directly follow the standard serialization protocol"
So this gives us a chance to write these two methods:
private void writeObject(ObjectOutputStream out) throws IOException;,
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;
inside the Class that implements Serializable and you get a hook to the serialization process. You can write your custom code inside these two methods and customize the standard behavior of serialization.
The main difference between Serializable and Externalizable is that Serializable automatically takes care of base classes for you. Externalizable leaves the entire job up to you.

How JVM handles marker interfaces internally? [duplicate]

This question already has answers here:
How Marker Interface is handled by JVM
(7 answers)
Closed 9 years ago.
Could any body explain me how JVM internally handles marker interfaces like:
Serializable
Just like any other interface... Marker interface is not a language construct, it's just a term used to describe an interface without any methods.
See: http://en.wikipedia.org/wiki/Marker_interface_pattern
The simple answer is it is not different from any other interface. There is absolutely no difference between a marker interface and other interface.
You may check Marker Interface in Java: what, why, uses, etc.
On a side note:-
If we take java.io.Serializable marker interface. Now this doesnot have any members defined in it.
So when a java class is to be serialized, then JVM should be intimiated in some way that if we want we can serialize this java class. So in this case, marker interfaces are used. So the java class which may be serialized has to implement the java.io.Serializable marker interface. Hence now we can initimate the JVM.

How does Externalizable differ from Serializable? [duplicate]

This question already has answers here:
What is the difference between Serializable and Externalizable in Java?
(11 answers)
Closed 9 years ago.
I read that
Externalizable provides us writeExternal() and readExternal() method which gives us flexibility to control java serialization mechanism instead of relying on Java's default serialization.
But If i implement Serializable and override readObject(), writeObject(), then does not it also means the same that I am customizing serialization process?
How does it differ?
THanks.
Difference between Externalizable and Serializable
In case of Serializable, default serialization process is used. while in case of Externalizable custom Serialization process is used which is implemented by application.
JVM gives call back to readExternel() and writeExternal() of java.io.Externalizalbe interface for restoring and writing objects into persistence.
Externalizable interface provides complete control of serialization process to application.
readExternal() and writeExternal() supersede any specific implementation of writeObject and readObject methods.
Though Externalizable provides complete control, it also presents challenges to serialize super type state and take care of default values in case of transient variable and static variables in Java. If used correctly Externalizable interface can improve performance of serialization process.
So go for Externalizable interface
When you have special requirements for the serialization of an object. For example, you may have some security-sensitive parts of the object, like passwords, which you do not want to keep and transfer somewhere. Or, it may be worthless to save a particular object referenced from the main object because its value will become worthless after restoring.
Official docs on Bean Persistence
Implement writeObject when you need to exercise greater control over what gets serialized when you need to serialize objects that default serialization cannot handle, or when you need to add data to the serialization stream that is not an object data member. Implement readObject to reconstruct the data stream you wrote with writeObject.
Use the Externalizable interface when you need complete control over your bean's serialization (for example, when writing and reading a specific file format). To use the Externalizable interface you need to implement two methods: readExternal and writeExternal. Classes that implement Externalizable must have a no-argument constructor.

Why does implementing Externalizable need a default public constructor?

We don't need it if we're implementing Serializable. So why this difference? How does it relate to the actual mechanism of Serialization?
A thorough explanation (although the grammar of the article might be improved) can be found on http://www.jusfortechies.com/java/core-java/externalization.php . The short answer, for future reference in case the linked page goes away:
Externalizable is an interface extending Serializable. Contrary to Serializable, though, objects are not restored by just reading the serialized bytestream, but the public constructor is called and only once the object is thus created, its state is restored. This makes restoring more efficient.
Edit: See also What is the difference between Serializable and Externalizable in Java? .
This is primarily used for caching purposes. In order to deserialize across streams, you will need to spell out how you want your object to be deserialized, hence the two methods provided by the contract in Externalizable interface: writeExternal and readExternal. Note that Externalizable extends Serializable, so you don't necessarily need to implement Serializable interface (although it's a marker interface and there are no methods to be actually implemented).
For a sample implementation, have a look at MimeType.
A public no-arg constructor is needed while using Externalizable interface.
Because in case of Serializable
readObject reads the required information from the ObjectInputStream
Serialization uses reflection mechanism to get the necessary fields and their corresponding values.
Serializable serializes all the data members (except static and transient).
But in case of Externalizable
No reflection mechanism used.
User doesn't serializes all data members.That's why to fetch values of the members which are not externalized public no arg constructor is required.

Categories