Showing posts from October 21, 2010

Shallow Copy Deep Copy Java example

Shallow Copy Deep Copy  Java example
Before knowing about Shallow Copy and Deep Copy you need to know about Clonable interface.

Clonable interface -
    Java has a clonable interface for make clone of any object.
    Clonable Interface is a marker interface.
    Clonable Interface has no method.
    Class which implements Clonable interface, JVM give the permission for making the clone of         this class.

Shallow Copy -  
    Clone can be archived by object.clone() method. It gives the Shallow copy of the object. In this process a new object is created That have all the value and instance variable. And if main object has any references to other object then the references are copied in the shallow copy.

Example -

class A implements Cloneable{

    A(String personName, Integer age){
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    private String personName = null;

Serialization In Java

Serialization In Java -
 In java, serialization is the process for writing the state of object in sequence
 of bytes stream. Serialization process for an object also called marshalling.

 How to serialize an object?
     Serializble is a marker interface, Through the implimantaion of Serializble interface
 any object can be serialize.

 There is no methods in Serializble interface, but Serializble interface tell to JVM
 that Object can be written in byte stream.

 If Serialized object have any transient variable. It means transient variable will not
 written in to byte stream.

Example of Serialization -

 class A implements Serializable{

    A(String personName, Integer age){
    private String personName = null;
    private transient Integer age = null;

    public Integer getAge() {
        return age;
    public String getPersonName() {
        return personName;

public class Test {
    public static void mai…

Recent Post

Recent Posts Widget