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.

16 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
    Replies
    1. Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training

      Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai Java Online Training Java Online Training Java Online Training Java Online Training

      Delete
    2. I have read your blog its very attractive and impressive. I like it your blog.

      Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

      Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

      Delete
  2. Replies
    1. Java Training Institutes Java Training Institutes Java EE Training in Chennai Java EE Training in Chennai Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

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

    ReplyDelete
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. Thanks for sharing this unique and informative content which provided me the required information.
    Java Training in Chennai | JAVA Course in Chennai

    ReplyDelete
  11. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Android training in Chennai |
    Android course in Chennai

    ReplyDelete
  12. Coding explanation with neat object name is excellent. Java seekers can take the java training and should learn advanced technology.

    ReplyDelete