Please navigate to the bottom of the page for Table of Contents

Sunday, June 19, 2011

JAVA serialization interview question

Question: Explain serialization in JAVA with a basic example and write the data to a file.

Serialization allows to save/restore objects state from some (generally persistent) storage medium. Serialization allows you to save the state of the object and all its instance variables except those explicitly marked as transient.

Basic serialization is managed with just two methods:

  • ObjectOutputStream.writeObject() - serialize objects and write them to a stream,
  • ObjectInputStream.readObject()  - read the stream and deserialize objects.

The java.io.ObjectOutputStream and java.io.ObjectInputStream classes are considered to be higher-level classes in the java.io package and we need to wrap them around lower-level classes, such as java.io.FileOutputStream and java.io.FileInputStream.

import java.io.*;

class Circle implements Serializable { }

public class SerializeCircle
{
public static void main(String[] args)
{
Circle c = new Circle();
try
{
FileOutputStream fs = new FileOutputStream("data.txt");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(c);
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}

try
{
FileInputStream fis = new FileInputStream("data.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
c = (Cat) ois.readObject();
ois.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}


Note that the Circle class implements the Serializable interface. Serializable is a marker interface; it has no methods to implement.

10 comments:

  1. Good question!!!

    I have also added some 2000 odd Core Java and J2EE questions in

    Java Interview Questions

    Have a look at them and reply.

    ReplyDelete
  2. Thanks for sharing informative article on java application development. Your post helped to understand the career in Java. Java Training in Chennai

    ReplyDelete
  3. Thanks for your informative article. Android SDK allows you to create stunning mobile application loaded with more features and enhanced priority. With basis on Java coding language, you can create stunning mobile application with ease. Best Android Training in Chennai

    ReplyDelete
  4. Thanks for your informative post on Java application development. This open source platform assists software developers to create stunning mobile application with ease. Further, they can make use of this platform at free of cost. J2EE Training in Chennai | JAVA Course in Chennai

    ReplyDelete
  5. Thanks for your post; selenium is most trusted automation tool to validate web application and browser. This tool provides precise and complete information about a software application or environment. Selenium Training in Chennai | Selenium Course in Chennai | Best Selenium training institute in Chennai

    ReplyDelete
  6. You can clear many of your doubts regarding data types in Core Java through Merit Campus, visit: http://java.meritcampus.com/core-java-topics/data-types-in-java.

    Not only data types, we also have each and every topic in Core Java with example for each. You can read lot of sessions and can write many practice tests in Merit Campus Java website. visit: http://java.meritcampus.com/ to know more.

    ReplyDelete
  7. The future of software testing is on positive note. It offers huge career prospects for talented professionals to be skilled software testers. Best software testing training institute in Chennai | Software Testing Training in Chennai | Software testing course in Chennai

    ReplyDelete
  8. Awesome blog! The author has done good job. It will really helpful for cracking Java interviews. I found one more easy and effective website relate to java interview Q/A’s. It will help for both core as well as advanced level, If you’re looking more Q/A’s for java then just look: Java Interview Questions

    ReplyDelete