Saturday, 15 April 2017

Javaforinterview: Serialization uses and example

Javaforinterview: Serialization uses and example: Serialization is the process of converting an object's and its references objects to a sequence of bytes, as well as the process of reb...

Serialization uses and example

Serialization is the process of converting an object's and its references objects to a sequence of bytes, as well as the process of rebuilding those bytes into  object .in simple words Converting an object to bytes and bytes back to object.
Serialization is used when you want to persist the object. It is also used by RMI to pass objects between JVMs, either as arguments in a method invocation from a client to a server or as return values from a method invocation. In general, serialization is used when we want the object to exist beyond the lifetime of the JVM.


Here are some uses of serialization
  • To persist data for future use.
  • To send data to a remote computer using such client/server Java technologies as RMI or socket programming.
  • To "flatten" an object into array of bytes in memory.
  • To store user session in Web applications.
  • To send objects between the servers in a cluster

A Java object is serializable if its class or any of its superclasses implements either the java.io.Serializable interface or its subinterface, java.io.ExternalizableDeserialization is the process of converting the serialized form of an object back into a copy of the object.




Example:

import java.io.Serializable;

public class Person implements Serializable {
        private String firstName;
        private String lastName;
        // stupid example for transient
        transient private Thread myThread;

        public Person(String firstName, String lastName) {
                this.firstName = firstName;
                this.lastName = lastName;
                this.myThread = new Thread();
        }

        public String getFirstName() {
                return firstName;
        }

        public void setFirstName(String firstName) {
                this.firstName = firstName;
        }

        public String getLastName() {
                return lastName;
        }

        public void setLastName(String lastName) {
                this.lastName = lastName;
        }

        @Override
        public String toString() {
                return "Person [firstName=" + firstName + ", lastName=" + lastName
                                + "]";
        }

}

The following code example show you how you can serializable and de-serializable this object.



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Main {
        public static void main(String[] args) {
                String filename = "time.ser";
                Person p = new Person("Sam", "R");

                // save the object to file
                FileOutputStream fos = null;
                ObjectOutputStream out = null;
                try {
                        fos = new FileOutputStream(filename);
                        out = new ObjectOutputStream(fos);
                        out.writeObject(p);

                        out.close();
                } catch (Exception ex) {
                        ex.printStackTrace();
                }
                // read the object from file
                // save the object to file
                FileInputStream fis = null;
                ObjectInputStream in = null;
                try {
                        fis = new FileInputStream(filename);
                        in = new ObjectInputStream(fis);
                        p = (Person) in.readObject();
                        in.close();
                } catch (Exception ex) {
                        ex.printStackTrace();
                }
                System.out.println(p);
        }
}