serialization

Serialization:-
              -This is the process to write the objects into the stream in such a way that further the objects can be reconstructed.
                Object serialization can be used in the networking where the one object will be passed across the network onto the different JVM. Through the serialization object can also be written into any file.
 

During the serialization along with the data the meta data also will be stored in the stream such as class name,field name,field type etc.
                   Note:- if after the serialization the .class file of the class is changed than process of deserialization will not work.
                  Note:- During the serialization the cloning of any singleton object is possible.
java.io.Serializable Interface:- By default the objects of the java classes are not serializable that means we cann't write those objects onto the stream.
                            Only the objects of the serializable classes that means classes that implements the Serializable interface can only be written onto the stream.
                            Serializable is marker interface that means it doesn't have any method.
                            The purpose of Serializable interface only to mark the class that their objects can be serialized.
                            NotSerializableException will be generated when we try to write the object onto the stream.whose class is not serializable.

ObjectOutputStream & ObjectInputStream:- These classes are the high level streams that are used to read and write the objects onto the stream.
Constructor:-
public ObjectOutputStream(OutputStream out)
public ObjectInputStream(InputStream in)

Methods:-
public void writeObject(Object ob)
               -this method is used to write the object onto the stream.
public Object readObject()
               -this method is used to read object from the stream.
if all objects already been retrieved then this method generates the EOFException.

              transient keyword can only be used with the non static variables. if we make any variable transient then their value will not be stored onto the stream.
                          transient variable and static variables can never be serialized.
declaration:-
                     transient String name;
Serialization in case of association:-In case of association along with the composite object their member objects are also be written onto the stream so both the classes also be the Serializable classes.
              class Student implements Serializable
              {
                   String name;
                   int rollno, marks;
                   Book b;
                   //constructor & methods...........
              }
Note:- the book class must be declared as serializable class.
              class Book implements Serializable
              {
                    String name;
                    int code,price;
                    ......................
              }

Serialization in case of inheritance:- In case of inheritance if the child class is the serializable class then their parent class need not to be declared as Serializable.
              class Employee
              {
                    int code,Salary;
              }
              class Manager extends Employee implements Serializable
              {
                   String name,project;
              }

Note:- the object of the Manager have the all fields (its own fields and the parent's fields) so that we don't need to made parent class serializable.

Externalization:- it is the inhanced version of serialization it provides a facility to customized the process of serialization and deserialization that means during the serialization we can change the value of object and can also change values during the deserialization.

java.io.Externalizable Interface:- This is the child interface of the Serializable interface. This is not the marker interface rather it provides following 2 methods:-
                   (1) public void readExternal(ObjectInput in)
                                            -This method automatically invoked from the readObject() of ObjectInputStream.
                   (2) public void writeExternal(ObjectOutput out)
                                           -This method automatically being invoked from the writeObject() of ObjectOutputStream.

Creating the Student class by implementing the Externalizable interface:-
          import java.io.*;
          class Student implements Externalizable 
          {
               String name;
               int rollno,marks;
               ........................
               ........................ 
          }

Difference b/w Serialization and Externalization:-

Externalization
Serialization
1: java.io.Externalizable interface is used.
1: java.io.Serializable interface is used.
2: it contains two methods.
2: Serializable is the marker interface.
3: Data can be changed.
3: Object’s data will written as it is.
4: Meta data will not written.
4: Meta data also be written.
5: During the read operation public default constructor will be used.
5: Same constructor will be used in deserialization.

java.io.File class:- instance of this class is used to represent the file or directly in the file system.
                       public File(String name)
                                  -This constructor will create the file object that represents the file or directory in file system but never create new file or directory. Argumented name can be the relative or absolute.
example:- 
                   new File("abc.txt");
                
                       public File(File folder,String name);
                                    -1st argument represents the folder in file system and 2nd argument is the file,in the argumented folder.
                  
java.lang.Process class:- an instance of this class represents a running process in the operating system.
              exec() of the java.lang.Runtime class is use to start the new process in the operating system.
              public Process exec(String exeName);
Methods of the Process class:-
(1) public InputStream getInputStream()
                    -This method returns the object of InputStream through which the output of the process can be received.
(2) public InputStream getErrorStream()
                   -This method is use to get the error of the process.
(3) public OutputStream getOutputStream()
                  -This method is use to get the OutputStream through which we can provide Input to the process.
(4) public void waitFor() throws InterruptedException
                  -This method will move our program in the waiting state upto the compilation of the process.
          
class ProcessTest
{
   public static void main(String[] args)
   {
          try
          {
               Runtime r=Runtime.getRuntime();
               Process p=r.exec("notepad.exe");
          }
          catch(Exception e)
          {}
   }
}

/*
   Process p=r.exec("notepad.exe ProcessTest.java");
                              or
   Process p=r.exec("javac DataIOTest.java");
*/

try above to process on your own programs and if you use linux than don't postfix notepad with .exe

Comments

Popular posts from this blog

Inter-Thread communication

define System.out.println()?

Reflection in java