Skip to main content

Diff_Serialization&Externalization

***********************************************************************************************************************
The process of converting system representation to network representation is called an marshalling.
The process of converting data from network representation to sysytem representation is called un marshalling.
************************************************************************************************************************

Serialization:
================
the process of seprating data from an object is called serialization.
or
-> The process of writing state of an object to a file is called Serialization.
-> But strictly speaking, it is a process of converting an object from java supported form to either file supprted form
     or network supported form or database supported form is called Serialization.
-> By using FileOutputStream and ObjectOutputStream, we can achieve Serialization.

It is meant for default Serialization.
Here everything takes care by JVM and Programmer doesn't have any control.
In Serialization, total object will be serialized always & it is not possible to serialize part of the object.
Relatively performance is low.
Serialization is best choice if we want to save total objects to the file.
Serialization Interface doesn't contain any methods.
So it is called as Marker Interface.******
transient keyword will play role in Serialization.

Step to create the serialization:
-----------------------------
step1 create the serialized class and its object.
   ex:- class employee inplements serializable{
        int emp id=101;
String emp name=ram;
----
-----
}
step2 create fileOutput stream.
FileOutputStream fos=new fileoutputStream("abc.txt");

Step3 create object outputStream
 for that we have to use the following constructor.
public objectoutputstream(FileOutPutStream fos)
ex: objectoutputStream oos=new objectoutputStream(fos);

step4 write serializable object in object outputstream:

we have to use this method for that.
public void writeobject(object obj)
ex: Employee emp=new employee();
oos.writeobject(emp);

Desirialization:
==================
The process of regenerating the object on the basis of data is called the deserialization.

Step to perform the des:
-------------------------------
step1 create fileInputStream.
ex: fileInputStream fis=new fileInputStream("abc.txt");

Step2 create objectInputStream:
 to create objectInputStream we have to use the following constructor.

public objectInputStream(FileInputStream fis)

ex: objectInputStream ois=new objectInputStream(fis);

Step3 read des obj from  objectInputStream:

for that we have to use the method.
  public object readObject()
ex:
Employee emp=(Employee)ois.read object();



Externalization:
=================
It is meant for customized Serialization.
Here everything takes care by Programmer and JVM doesn't have any control.
In Externalization, based on our requirement we can save either total object or part of the object.
Relatively performance is high.
Externalization is best choice if we want to save part of the object to the file.
Externalization Interface contains 2 methods. 1. writeExternal(-) and 2. readExternal(-)
So it is not a Marker Interface.
transient keyword will not play any role in Externalization.


Marker Interface:
==================
-> Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface.
-> Example of marker interface is Serializable, Clonnable and Remote interface.
-> It looks they are used to indicate something to compiler or JVM.
-> So if JVM sees a class is Serializable it done some special operation on it,
     similar way if JVM sees one class is implement Clonnable it performs some operation to support cloning.
     Same is true for RMI and Remote interface. So in short Marker interface indicate, signal or a command to Compiler or JVM.
-> After introduction of Annotation on Java5, Annotation is better choice than marker interface and JUnit is a perfect example of using Annotation.
->  In summary marker interface in Java is used to indicate something to compiler, JVM or any other tool but Annotation is better way of doing same thing.


Comments

Popular posts from this blog

Mockito interview Questions

1.       Question 1. What Is Mockito? Answer : Mockito allows creation of mock object for the purpose of Test Driven Development and Behavior Driven development. Unlike creating actual object, Mockito allows creation of fake object (external dependencies) which allows it to give consistent results to a given invocation. 2.       Question 2. Why Do We Need Mockito? What Are The Advantages? Answer : Mockito differentiates itself from the other testing framework by removing the expectation beforehand. So, by doing this, it reduces the coupling. Most of the testing framework works on the "expect-run-verify". Mockito allows it to make it "run-verify" framework. Mockito also provides annotation which allows to reduce the boilerplate code. 3.       Question 3. Can You Explain A Mockito Framework? Answer : In Mockito, you always check a particular class. The dependency in that class is injected using m...

JAVA Expert Interview Questions Answers 2017

Java Basics ::  Interview Questions and Answers Home  »  Interview Questions  »  Technical Interview  »  Java Basics  » Interview Questions 1.     What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 2.     What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. 3.  ...

Java Example Program to Convert List to Set

import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class ListToSet {  /**   * @author Amarjit Kumar   * @category interview questions   *   * Description: Convert List to set in java with example program   *   */  public static void main(String[] args) {   ArrayList<String> arrList= new ArrayList<>();   arrList.add("Java");   arrList.add("Java");   arrList.add("List to String");   arrList.add("Example Program");   Set<String> strSet = new HashSet<String>(arrList);   System.out.println(strSet);   System.out.println(arrList);  } } /*  * Java program to convert list to set. Convert ArrayList of string to HashSet  * in java example program How to convert List to Set in java Set<String> strSet  * = new HashSet<String>(arrList); HashSet having a constructor which will take  * list as an ar...