Friday, 11 September 2015

Difference between Parcelable and Serializable.


Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations. The Problem is that it is used reflection so that it is a slow process. It is very easier to use. This approach used for while sending data over the network or sending object from one activity to another activity in android.
Serialization is the process of converting an object's state (including its references) to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time.Every serializable class is assigned a version identifier called a serialVersionUID

Uses of Serialization


  • Send data to a remote computer using such client/server Java technologies as RMI or socket programming
  • Send objects between the servers
  • Persist data for future use
  • Store serialized object in Disk or database as Blob
  • Send Object from one activity to another activity in android

Implementation


import java.io.Serializable;
import java.util.ArrayList;
import java.util.TreeMap;

import android.os.Parcel;
import android.os.Parcelable;

public class MyObjects implements Serializable {

private String name;
private int age;

public ArrayList<String> address;

public MyObjects(String name, int age, ArrayList<String> address) {
    super();
    this.name = name;
    this.age = age;
    this.address = address;
}

public ArrayList<String> getAddress() {
    if (!(address == null))
        return address;
    else
        return new ArrayList<String>();
}

public String getName() {
    return name;

}

public String getAge() {
    return age;
}

}

Parcelable :
Android Parcelable implementation allows objects to read and write from Parcels which can contain flattened data inside message containers.
If a developer wants to convert a Java object into Parcelable, then the best way to do so is by implementing the Parcelable interface and overriding the writeToParcel() methods in its own class. The first step is to override the writeToParcel() method and write all object members into parcel objects. The second is to create a static Parcelable.Creator object to de-serialize the Java object.

Implementation
 
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
 
public class Student implements Parcelable{
 
    String mSName;
    int mSAge;
    String mSAddress;
    String mSCourse;
 
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
 
    /**
    * Storing the Student data to Parcel object
    **/
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mSName);
        dest.writeInt(mSAge);
        dest.writeString(mSAddress);
        dest.writeString(mSCourse);
    }
 
    /**
    * A constructor that initializes the Student object
    **/
    public Student(String sName, int sAge, String sAddress, String sCourse){
        this.mSName = sName;
        this.mSAge = sAge;
        this.mSAddress = sAddress;
        this.mSCourse = sCourse;
    }
 
    /**
    * Retrieving Student data from Parcel object
    * This constructor is invoked by the method createFromParcel(Parcel source) of
    * the object CREATOR
    **/
    private Student(Parcel in){
        this.mSName = in.readString();
        this.mSAge = in.readInt();
        this.mSAddress = in.readString();
        this.mSCourse = in.readString();
    }
 
    public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
 
        @Override
        public Student createFromParcel(Parcel source) {
            return new Student(source);
        }
 
        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
    };
}


Differences between Serialization and Parcelable.
  • Parcelable and Serialization are used for marshaling and unmarshaling Java objects.
  • Parcelable is faster than Serialization makes it a preferred choice of approach while passing an object.
  • Parcelable interface takes more time for implemetation compared to serializable interface
  • Serializable interface is easier to implement
  • Serializable interface create a lot of temporary objects and cause quite a bit of garbage collection
  • Parcelable array can be pass via Intent in android
  • Serialization is a marker interface, which implies the user cannot marshal the data according to their requirements. In Serialization, a marshaling operation is performed on a Java Virtual Machine (JVM) using the Java reflection API

No comments:

Post a Comment