Friday, June 3, 2016

Closeable and Flushable Interface JAVA

View Priya Saini's LinkedIn profile View Priya Saini's profile

Closeable and Flushable interface

Closeable and Flushable interfaces were introduced with JDK 1.5 version and part of java.io package.

1. Closeable interface (of Closeable Flushable Java)


The Closeable interface includes only one abstract method, close().
When close() method is called, the system resources held by the stream object
are released and can be used by other part of the program (avoids memory leaks).
The close method is invoked to release resources that the object is holding


Also if the super class implements this interface, the sub class can use this method.
For example, the InputStream implements this method and its subclass FileInputStream can use close() method.
So, all the streams can use close() method as the super classes of all streams,
InputStream, OutputStream, Reader and Writer, implement Closeable interface.

Syntax:

public interface Closeable extends AutoCloseable{
}

Throws:
    IOException - if an I/O error occurs


2. Flushable interface (of Closeable Flushable Java)

The Flushable interface includes only one method – flush().
Many destination streams implement this interface and overrides the flush() method.
this method is called to flushed out the data to the destination file to write that held in buffers
Following are a few classes signature that implements Closeable Flushable Java (either one or both)

The close() and flush() methods throw a checked exception IOException and must be handled when these methods are used.


Examples:-   

public abstract class InputStream extends Object implements Closeable
public abstract class OutputStream extends Object implements Closeable, Flushable
public abstract class Reader extends Object implements Readable, Closeable
public abstract class Writer extends Object implements Appendable, Closeable, Flushable
public class PrintStream extends FilterOutputStream implements Appendable, Closeable


Thursday, June 2, 2016

Serialization & Externalization in Java

java.io.Serializable interface

Serializable 

Serializable Interface is a Marker Interface. Hence there is no method in Serializable interface.Serialization is a process in which state of Object will be saved into a byte stream(binary format) which can be persisted into disk or sent over network to any other running JVM.
It is mainly used in Hibernate, RMI, JPA technologies.

The reverse operation of serialization is called deserialization.
The String class and all the wrapper classes implements java.io.Serializable interface by default.It must be implemented by the class whose object you want to persist.

Advantage of Java Serialization

It is mainly used to sent object's state on the network (known as marshaling).
Byte stream can be used for different purpose.
1.Write to Disk
2.Store in Memory
3.Sent byte stream over network to other platform
4.Save byte stream in DB(As BLOB)



Example:

    import java.io.*; 
    class Persist{ 
     public static void main(String args[])throws Exception{ 
      Student s1 =new Student(211,"ravi"); 
     
      FileOutputStream fout=new FileOutputStream("f.txt"); 
      ObjectOutputStream out=new ObjectOutputStream(fout); 
     
      out.writeObject(s1); 
      out.flush(); 
      System.out.println("success"); 
     } 
    } 
   
Output: success

Java Serialization with array or collection

Rule: In case of array or collection, all the objects of array or collection must be serializable. If any object is not serialiizable, serialization will be failed.


Externalizable:

Syntax :
public interface Externalizable extends Serializable

It is not a marker interface.As name suggest it is externalilizing your serialization.If you want to customize your serialization mechanism then you can use it.It uses custom written mechanism to perform marshalling and unmarshalling of objects.Externalizable interface extends Serializable interface.
If you implement this interface then you need to override following methods.

    @Override
    public void readExternal(ObjectInput arg0) throws IOException,

            ClassNotFoundException {

    }

    @Override
    public void writeExternal(ObjectOutput arg0) throws IOException {

    }

Now lets see how serialization happens:

At sender side:
JVM checks if class implements externalizable or not.If it does then serialize object using writeExternal() method.
If it does not implement externalizable but implements serializable , object is serialized using ObjectOutputStream.

At receiver side:
When object is reconstructed and it is externalizable , an instance is created using no args constructor and
readExternal is called.If it is not externalizable but serializable , object is reconstructed using ObjectInputStream.

The Externalizable interface provides two methods:
public void writeExternal(ObjectOutput out) throws IOException
public void readExternal(ObjectInput in) throws IOException



Difference between serialization and externalization
Externalizable provides us writeExternal() and readExternal() method which
gives us flexibility to control java serialization
mechanism instead of relying on Java's default serialization.


Can you Customize Serialization process or can you override default Serialization process in Java?
ObjectOutputStream.writeObject (saveThisobject) is invoked and for reading object ObjectInputStream.readObject()
is invoked but there is one more thing which JVM provides you is to define these two method in your class.
If you define these two methods in your class then JVM will invoke these two methods instead of applying default serialization mechanism.


Which kind of variables is not serialized during Java Serialization?
This question asked sometime differently but the purpose is same whether Java developer knows specifics about static and transient variable or not.Since static variables belong to the class and not to an object they are not the part of
the state of object so they are not saved during Java Serialization process.
As Java Serialization only persist state of object and not object itself. Transient variables are also not included in java serialization process and are not  the part of the object’s serialized state.

Read More: https://www.journaldev.com/2452/serialization-in-java

Referred Sites :

1.http://www.javatpoint.com/serialization-in-java
2.http://javarevisited.blogspot.com/2011/04/top-10-java-serialization-interview.html#ixzz4AQVlkNqa
3.http://www.java2blog.com/2014/02/externalizable-in-java.html

Cloneable Interface in Java


Introduction

 

The object cloning is a way to create exact copy of an object. For this purpose, clone() method of Object class is used to clone an object.

The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create.If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException.The clone() method is defined in the Object class. 
Syntax of the clone() method is as follows:

protected Object clone() throws CloneNotSupportedException 

 

Why use clone() method

The clone() method saves the extra processing task for creating the exact copy of an object.If we perform it by using the new keyword,it will take a lot of processing to be performed that is why we use object cloning.

 

Advantage of Object cloning

Less processing task.
 
1.First statement guarantees that cloned object will have separate memory address assignment.
2.Second statement suggest that original and cloned objects should have same class type, but it is not mandatory.
3.Third statement suggest that original and cloned objects should have be equal using equals() method, but it is not mandatory.


Does clone object and original object point to the same location in memory

The answer is no. The clone object has its own space in the memory where it copies the content of the original object.
That’s why when we change the content of original object after cloning, the changes does not reflect in the clone object.



Shallow Copy

 

Whenever we use default implementation of clone method we get shallow copy of object means it create new instance and copy all the field of object to that new instance and return it as object type we need to explicitly cast it
back to our original object. This is shallow copy of the object. clone() method of the object class support shallow copy of the object. If the object contains primitive as well as non primitive or reference type variable In shallow copy,
the cloned object also refers to the same object to which the original object refers as only the object references gets copied and not the referred objects themselves. That's why the name shallow copy or shallow cloning in Java.
If only primitive type fields or Immutable objects are there then there is no difference between shallow and deep copy in Java.




Example:
In this figure, the MainObject1 have fields "field1" of int type, and "ContainObject1" of ContainObject type.When you do a shallow copy of MainObject1, MainObject2 is created with "field3" containing the copied value of
"field1" and still pointing to ContainObject1 itself. Observe here and you will find that since field1 is of primitive type, the values of it are copied to field3 but ContainedObject1 is an object, so MainObject2 is still pointing to ContainObject1. So any changes made to ContainObject1 in MainObject1 will reflect in MainObject2.



Deep Copy


Whenever we need own meaning of copy not to use default implementation we call it as deep copy, whenever we need deep copy of the object we need to implement according to our need. So for deep copy we need to ensure allthe member class also implement the Cloneable interface and override the clone() method of the object class.After that we override the clone() method in all those classes even in the classes where we have only primitivetype members otherwise we would not be able to call the protected clone() method of Object class on the instances of those classes inside some other class. It’s typical restriction of the protected access



Example:
In this figure, the MainObject1 have fields "field1" of int type, and "ContainObject1" of ContainObject type. When you do a deep copy of MainObject1, MainObject2 is created with "field3" containing the copied value of "field1" and "ContainObject2" containing the copied value of ContainObject1.So any changes made to ContainObject1 in MainObject1 will not reflect in MainObject2. 

Practical Implementation:

Department class has two attributes. id and name.
public class Department
{
    private int id;
    private String name;

    public Department(int id, String name)
    {
        this.id = id;
        this.name = name;
    }
    //Accessor/mutators methods will go there
}


Employee class with 3 attributes. Id, name and department.
public class Employee implements Cloneable{

    private int empoyeeId;
    private String employeeName;
    private Department department;

    public Employee(int id, String name, Department dept)
    {
        this.empoyeeId = id;
        this.employeeName = name;
        this.department = dept;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    
    //Accessor/mutators methods will go there
}

Clone the Employee class
public class TestCloning {

    public static void main(String[] args) throws CloneNotSupportedException
    {
        Department dept = new Department(1, "Human Resource");
        Employee original = new Employee(1, "Admin", dept);
        //Lets create a clone of original object
        Employee cloned = (Employee) original.clone();
        //Let verify using employee id, if cloning actually workded
        System.out.println(cloned.getEmpoyeeId());

        //Verify JDK's rules

        //Must be true and objects must have different memory addresses
        System.out.println(original != cloned);

        //As we are returning same class; so it should be true
        System.out.println(original.getClass() == cloned.getClass());

        //Default equals method checks for refernces so it should be false. If we want to make it true,
        //we need to override equals method in Employee class.
        System.out.println(original.equals(cloned));
    }
}

Output:
1
true
true
false


We successfully cloned the Employee object. But, remember we have two references of same object and now both will change the state of object in different parts of application.Lets see: 
public class TestCloning {

    public static void main(String[] args) throws CloneNotSupportedException {
        Department hr = new Department(1, "Human Resource");
        Employee original = new Employee(1, "Admin", hr);
        Employee cloned = (Employee) original.clone();

        //Let change the department name in cloned object and we will verify in original object
        cloned.getDepartment().setName("Finance");

        System.out.println(original.getDepartment().getName());
    }
}
Output: Finance


Referred Sites : 

http://www.jusfortechies.com/java/core-java/deepcopy_and_shallowcopy.php 

http://java67.blogspot.in/2013/05/difference-between-deep-copy-vs-shallow-cloning-java.html
http://www.javatpoint.com/object-cloning
http://howtodoinjava.com/core-java/cloning/a-guide-to-object-cloning-in-java/