Skip to main content

Serialization and Deserialization Interview Questions in Java


Serialization and Deserialization




1. What is the serialization?

Serialization is a process converting an object into a byte array. This byte array represents the class, version and internal state of the object. JVM can use this byte array to transmit/read the object over a network.

2. What is the purpose of serialization?

Some of the uses of serialization are:
1.     Communication: It is used for transmitting an object over network between two machines.
2.     Persistence: We can store the object’s state in a database and retrieve it from database later on.
3.     Caching: Serialization can be used for caching to improve performance. We may need 10 minutes to build an object, but it may take just 10 seconds to de-serialize the object.
4.     Cross JVM Synchronization: It can be used in same way across multiple JVM that follow different architecture.

3. What is Deserialization?

Deserialization is the process of reconstructing the object from the serialized state. It is the reverse process of serialization.

4. What is Serialization and Deserialization conceptually?

Serialization is to convert Object data into a stream of bytes Deserialization is to convert a stream of bytes back into a copy of the original object.

5.Why do we mark a data member transient?

Member variables of an object are marked transient to indicate that they should not be serialized. During serialization process the transient variables are not considered part of the persistent state of an object.

6. Is it allowed to mark a method as transient?

No, Java does not allow marking a method as transient. The transient keyword is valid only for member variables.

7. How does marking a field as transient makes it possible to serialize an object?

Let say we have a class ABC that implements Serializable interface, but it contains a member variable object of class XYZ that does not implement Serializable interface. Due to this it is not possible to Serialize the class ABC. To solve this issue, we can mark the member variable XYZ as Transient in class ABC. This will allow us to serialize the class ABC.

8. What is Externalizable interface in Java?

Externalizable interface extends Serializable interface in Java. It is used for giving the Class control over saving and restoring the contents of its instances. A class implements methods writeExternal() and readExternal() to store and restore the object.

9. What is the difference between Serializable and Externalizable interface?

Serializable is a marker interface but Externalizable is not a marker interface. When we implement Serializable interface, the class is serialized automatically by default. We can override writeObject() and readObject() methods to control more complex object Serialization process. In case of Externalizable, we use readExternal() and writeExternal() methods to give control to class for class's serialization process. Serializable interface is based on recursive algorithm. Serializable gives you two options. One option is to provide custom way of serialization, the other default way. In Externalizable, you have to always implement readExternal() and writeExternal() methods. A public no-arg constructor is needed while using Externalizable interface. In Serialization, we need to define serialVersionUID. If it is not explicitly defined it will be generated automatically based on all the fields, methods of the class.





Comments

Popular posts from this blog

DOCKER Interview questions

DOCKER 1. What is Docker? Docker is Open Source software. It provides the automation of Linux application deployment in a software container. We can do operating system level virtualization on Linux with Docker. Docker can package software in a complete file system that contains software code, runtime environment, system tools, & libraries that are required to install and run the software on a server. 2. What is the difference between Docker image and Docker container? Docker container is simply an instance of Docker image. A Docker image is an immutable file, which is a snapshot of container. We create an image with build command. When we use run command, an Image will produce a container. In programming language, an Image is a Class and a Container is an instance of the class. 3. How will you remove an image from Docker? We can use docker rmi command to delete an image from our local system. Exact command is: % docker rmi <Image Id> If we want to fin...

Microservices Interview Questions

Microservices Interview Questions 1. What is a Microservice in Java? A Microservice is a small and autonomous piece of code that does one thing very well. It is focused on doing well one specific task in a big system. It is also an autonomous entity that can be designed, developed and deployed independently. Generally, it is implemented as a REST service on HTTP protocol, with technology-agnostic APIs. Ideally, it does not share database with any other service. 2. What are the benefits of Microservices architecture? Microservices provide many benefits. Some of the key benefits are: 1.      Scaling : Since there are multiple Microservices instead of one monolith, it is easier to scale up the service that is being used more. Eg. Let say, you have a Product Lookup service and Product Buy service. The frequency of Product Lookup is much higher than Product Buy service. In this case, you can just scale up the Product Lookup service to run on powerful ha...

Method Overloading and Overriding Interview Questions

Method Overloading and Overriding 1. What is the another name of Method Overloading? Method Overloading is also known as Static Polymorphism. 2.In java, How will we implement method overloading? We known, a class can have multiple method with same name but different arguments. It is called Method Overloading. To implement method overloading we have to create two methods with same name in a class and do one/more of the following: 1.      Different number of parameters 2.      Different data type of parameters 3.      Different sequence of data type of parameters 3. What kinds of argument variation are allowed in Method Overloading? Method Overloading only allows two methods with same name to differ in: 1.      Number of parameters 2.      Data type of parameters 3.      Sequence of data type of parameters 4. It is not p...