Saturday 25 February 2012

How to create immutable classes in java


Immutable class is the class whose value cannot be changed throughout the life cycle.
Ex:String
We cannot change the content of String class, everytime new reference is created when we change the content.

To create custom immutable  class in java
  1. Class should be made final so that no class can extend it. 
  2. Declare all variables as final
  3. Provide constructors to set the values
  4.  There should not be any public set method which can change the state of 
    the object.
  5. provde getter method




package com.usr.general;

final class Person {
       final int age;
       final String name;

       public Person(int age, String name) {
              this.age = age;
              this.name = name;

       }

       public int getAge() {
              return age;
       }

       public String getName() {
              return name;
       }
}

public class ImmutableDemo {
       public static void main(String[] args) {
              Person p = new Person(38, "sachin");
              System.out.println(p.age);
              System.out.println(p.name);
       }
}

OUTPUT
38
sachin

Friday 24 February 2012

java program to convert char array to String


package com.usr.strings;

public class ConvertCharArrayToString {
       public static void main(String[] args) {
              char[] charArr = { 'w', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o',
                           ' ', 'j', 'a', 'v', 'a', '1', '1', 's' };

              String str = new String(charArr);
              System.out.println("converted String is:" + str);

              String str1 = String.valueOf(charArr);
              System.out.println("covnverted String is:" + str1);
       }
}

OUTPUT
converted String is:welcome to java11s
covnverted String is:welcome to java11s



java program to convert String to char array


package com.usr.strings;

public class ConvertStringToCharArray {
       public static void main(String[] args) {
              String str="welcome to java11s.blogspot.in";
              char[] charArr=str.toCharArray();

              for(char temp:charArr){
                     System.out.println(temp);
              }
       }
}

OUTPUT
w
e
l
c
o
m
e

t
o

j
a
v
a
1
1
s
.
b
l
o
g
s
p
o
t
.
i
n

Deexternalization in java


package com.usr.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class DeExternalizationDemo {
       public static void main(String[] args) {
              Employee emp = new Employee();
              File file = new File("employee.ser");
              FileInputStream fis = null;
              ObjectInputStream ois = null;
              try {
                     fis = new FileInputStream(file);
                     ois = new ObjectInputStream(fis);
                     emp.readExternal(ois);
              } catch (IOException ex) {
                     ex.printStackTrace();
              } catch (ClassNotFoundException ex) {
                     ex.printStackTrace();
              } finally {
                     try {
                           if (ois != null) {
                                  ois.close();
                           }
                     } catch (IOException ex) {
                           ex.printStackTrace();
                     }
                     try {
                           if (fis != null) {
                                  fis.close();
                           }
                     } catch (IOException ex) {
                           ex.printStackTrace();
                     }

              }
              System.out.println("Object DeExternalized........");
              System.out.println(emp.name);
              System.out.println(emp.age);
              System.out.println(emp.salary);

       }
}

output 
Object DeExternalized........
name:Alex
age:26
salary:34567.8

Externalization in java



Limitations of Serialization
1)File size is very high
2)Customization due to transient which  is not effective because we get “null” in place of transient attributes.
3)while customizing we also get a metainformation of the file which includes when created who are eligible to read it etc: which is against data security.


Inorder to address these limitations of serialization,sun people came up with another I/O process named Externalization,which refers to dumping the state of an object in a permanent media using the interface Externalizable.

Externalizable is a sub-interface to Serializable but it is not a marker interface because it has two unimplemented methods readExternal() and writeExternal() which should be implemented by the classes which use Externalizable interface.

The process of externalization is same as that of serialization except the following:
1)Implementing writeExternal(ObjectOutput oout) & readExternal(ObjectInput oin) methods of Externalizable interface in a class which we want to externalize
2)Employing writeExternal() and readExternal() in place writeObject() & readObject() respectively.In block of writeExternal(),keep the attributes which we like to externalize
3)customization is very easy incase of externalization because whatever attributes we want to keep away from externalization just don’t keep them inside writeExternal() method block
4) In writeExternal() method block we make attributes externalized by using corresponding methods for different types of data. We should take care of IOException while using this method.

   Ex:writeInt(i)----------àfor integers
      writeDouble(d)------àfor doubles
      writeUTF(s)-------àfor strings
      writeObject(i)-------àfor derived attributes other than string& wrapper         
                              classes
UTF--àUniversal Text Format

5) using readExternal() method we can read the states of the object returned using the corresponding read methods stated above
6)flusing and closing operations are same to that of serialization
7)All the rules of derived attributes & Inheritance applied for serialization are also valid in case of Externalization.
8)If a class uses any derived attributes other than String & all Wrapper class attributes then that particular class also should be implemented with either Externalizable or Serializable interface.If we don’t do so, we get NotSerializableException

The main advantages of externalization over serialization are:
1)File size is highly reduced(nearly 1/3)
2) customization is very easy and more effective.


Java program to demonstrate Externalization
package com.usr.io;

import java.io.Externalizable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

class Employee implements Externalizable {
       String name;
       double salary;
       int age;
       long cardNo;

       public void writeExternal(ObjectOutput out) throws IOException {
              out.writeUTF(name);
              out.writeInt(age);
              out.writeDouble(salary);

       }

       public void readExternal(ObjectInput in) throws IOException,
                     ClassNotFoundException {
              name = in.readUTF();
              age = in.readInt();
              salary = in.readDouble();

       }
}
while writing whatever order we follow the same order must be followed in reading also.

public class ExternalizationDemo {
       public static void main(String[] args) {
              Employee emp = new Employee();
              emp.name = "Alex";
              emp.age = 26;
              emp.salary = 34567.8;
              emp.cardNo = 65754534;
              File file = new File("employee.ser");
              FileOutputStream fos = null;
              ObjectOutputStream oos = null;
              try {
                     fos = new FileOutputStream(file);
                     oos = new ObjectOutputStream(fos);
                     emp.writeExternal(oos);
                     System.out.println("object persisted");
              } catch (IOException ex) {
                     ex.printStackTrace();
              } finally {
                     try {
                           if (oos != null) {
                                  oos.flush();
                                  oos.close();
                           }
                     } catch (IOException ex) {
                           ex.printStackTrace();
                     }
                     try {
                           if (fos != null) {
                                  fos.flush();
                                  fos.close();
                           }
                     } catch (IOException ex) {
                           ex.printStackTrace();
                     }
              }
       }
}