Java Input Output - IO Streams Interview Questions Answers - 3

Question: 11

What are the advantages and disadvantage of serialization?

The advantages of serialization are

It is easy to use and can be customized.

The serialized stream can be encrypted, authenticated and compressed, supporting the needs of secure Java computing.

There are simply too many critical technologies that rely upon serialization, including RMI, JavaBeans and EJB.

Serialized classes can support coherent versioning and are flexible enough to allow gradual evolution of your application’s object schema.

Serialization can also be used as a mechanism for exchanging objects between Java and C++ libraries, using third party vendor libraries within C++.

 

However, serialization has some disadvantages too

Since serialization does not offer any transaction control mechanism per se, it is not suitable for use within applications needing concurrent access without making use of additional APIs.

It should ideally not be used with large sized objects as it offers significant overhead. Large objects also significantly increase the memory requirements of your application since the object input/output streams cache live references to all objects written to or read from the stream until the stream is closed or reset.

The serialization interface does not offer fine grained control over object access although you can somewhat circumvent this issue by implementing the complex Externalizable interface, instead.

Question: 12

What is the difference between serializable and externalizable interface?

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 serialization process.

When you use Externalizable interface, you can have a complete control over your class’s serialization process.

Question: 13

What happens to the static fields of a class during serialization?

There are three exceptions in which serialization does not necessarily read and write to the stream. These are

Serialization ignores static fields, because they are not part of any particular state.

Base class fields are only handled if the base class itself is serializable.

Transient fields

Question: 14

What interface must be 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.

Question: 15

How to make a class or a bean serializable?

By implementing either the java.io.Serializable interface, or the java.io.Externalizable interface.

As long as one class in a class’s inheritance hierarchy implements Serializable or Externalizable, that class is serializable.

Related Questions