Skip to main content

What is Java Serialization?

Java has classes, A class has properties. These properties with values called object or state of object. Now we want to save this state of object into file or database. With java serialization we can convert object state into data stream and this data stream would be save into file system or database or create object replica into another system.

Java Serialization Covered Two Process -
  1. Serialization: Convert object state into data stream.
  2. Deserialization: Convert data stream into object state. 

Simple Java Code Example: 

import java.io.Serializable;

public class Student implements Serializable{

 private String name;
 private int age;
 
 public Student(String name, int age) {
  this.name = name;
  this.age = age;
 }
 
 public void print() {
  System.out.println("Name:"+name+" Age:"+age);
 } 
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Main {

 private static void serializeStudent() {
  try {
   Student stu = new Student("Ravi", 22);
   
   FileOutputStream fos = new FileOutputStream("student.obj");
   ObjectOutputStream oos = new ObjectOutputStream(fos);
   oos.writeObject(stu);
   
   System.out.println("student serialized");
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
 
 private static Student deserializeStudent() {
  Student stu = null;
        try{
         FileInputStream fis = new FileInputStream("student.obj");
         ObjectInputStream ois = new ObjectInputStream(fis);
         stu = (Student)ois.readObject();
        }catch(Exception e){
         e.printStackTrace();
        }
  return stu;
 }
 
 public static void main(String[] args) {
  Main.serializeStudent();
  Main.deserializeStudent().print();
 }
 
}

Comments

Popular posts from this blog

Java Array, Multi Dimensional Array, Anonymous Array and Arrays Util Class

Array An array is a index based container object. The length of an array fixed at time of array creation. An Array holds a fixed number of values of a single type . One Dimensional Array Array can be created by two ways. Syntex: //1st way arraytype[] name = new arraytype[length]; //2nd way arraytype name[] = new arraytype[length]; 1st way is more readable. Examples: //charecyter Array Example: //creating Array of char, length 5 char[] charArray = new char[5]; //Initialize array elements charArray[0] = 'H'; charArray[1] = 'E'; charArray[2] = 'L'; charArray[3] = 'L'; charArray[4] = 'O'; //creating Array of int, length 4 int[] intArray = new int[4]; //Initialize array elements intArray[0] = 2; intArray[1] = 0; intArray[2] = 1; intArray[3] = 5; //creating Array of string, length 7 String[] weeks = new String[7]; //Initialize array elements weeks[0] = "mon"; weeks[1] = "tue"; weeks[2] = "wed"; w

String - A story of very special class in java

String is most used java class in any java project. Java treated String as special class. Lets see, what so special in String. String Basics String is group of characters. char[] chrs = {'i',' ','a','m',' ','h','e','r','o','.'}; String str = new String(chrs); System.out.println(str); /* Output: i am hero. */ String is an immutable class. Immutable class:  A java class which object can not be modified if once created. Because String class is immutable, String is final in nature, its mean you can not modify string object once you created. Default value of string is null. Simplest way to initialize String is //object created in String Literal Pool String str = "hello world"; Above example known as String literal, All string literals in Java programs, such as "hello world", are object of String class. Another way to create String object is //object created in Heap m