Serialization Deserialization & Externalization | Java Interview Questions and Answer | Code Decode

Описание к видео Serialization Deserialization & Externalization | Java Interview Questions and Answer | Code Decode

In this video of code decode core java we have covered serialization, deserialization and externalization
Udemy Course of Code Decode on Microservice k8s AWS CICD link:
https://openinapp.co/udemycourse

Course Description Video :
https://yt.openinapp.co/dmjvd

What is Serialization
Serialization is the conversion of a Java object into a static stream (sequence) of bytes, which we can then save to a database or transfer over a network.

Classes that are eligible for serialization need to implement a special marker interface, Serializable. The JVM allows special privileges to the class which implements the Serializable Interface.

Byte stream is platform-independent. This means that once you have a stream of bytes you can convert it into an object and run it on any kind of environment.

A class to be serialized successfully, two conditions must be met −

The class must implement the java.io.Serializable interface.
All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.
static fields belong to a class (as opposed to an object) and are not serialized


What is Deserialization
Deserialization is precisely the opposite of serialization. With deserialization, you start with a byte stream and re-create the object you previously serialized in its original state. However, you must have the definition of the object to successfully re-create it.


FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();

What is Externalization?
Externalization in Java is used whenever you need to customize the serialization mechanism.

In serialization, the Java Virtual Machine is totally responsible for the process of writing and reading objects. This is useful in most cases, as the programmers do not have to care about the underlying details of the serialization process. However, the default serialization does not protect sensitive information such as passwords and credentials, or what if the programmers want to secure some information during the serialization process?

Thus externalization comes to give the programmers full control in reading and writing objects during serialization. JVM has no control over it. The complete serialization control goes to the application.

Based on our requirements, we can serialize either the whole data field or a piece of the data field using the externalizable interface which can help to improve the performance of the application.

Externalizable interface internaly extends Serializable interface

Externalizable interface is not a marker interface like Serializable interface. So, it provides two methods that are as follows:

void readExternal(ObjectInput inStream) - we call readExternal() method when we want to read an object’s fields from a stream. We need to write logic to read an object’s fields inside the readExternal() method. The readExternal() method throws IOException when an I/O error occurs. If the class of object being restored does not find, ClassNotException will be thrown.

void writeExternal(ObjectOutput outStream): writeExternal() method is used when we want to write an object’s fields to a stream. We need to write the logic to write data fields inside writeExternal() method. This method can throw an IOException when an I/O error occurs.



Most Asked Core Java Interview Questions and Answers :    • Core Java frequently asked Interview ...  

Advance Java Interview Questions and Answers :    • Advance Java Interview Questions  

Java 8 Interview Questions and Answers :    • Java 8 Interview Questions(New Features)  

Hibernate Interview Questions and Answers :    • Hibernate Interview Questions Java  

Spring Boot Interview Questions and Answers :    • Advance Java Interview Questions  

Angular Playlist :    • Angular Course Introduction || Angular 8  


SQL Playlist :    • SQL Interview Questions and Answers  

GIT :    • GIT  

Subscriber and Follow Code Decode
Subscriber Code Decode : https://www.youtube.com/c/CodeDecode?...
LinkedIn :   / codedecodeyoutube  
Instagram :   / codedecode25  

#singletondoublecheckedlocking #codedecode #javainterviewquestion

Комментарии

Информация по комментариям в разработке