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.

81 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
    Replies
    1. Hello There,


      Hip Hip Hooray! I was always told that slightly slow in the head, a slow learner. Not anymore! It’s like you have my back. I can’t tell you how much I’ve learnt here and how easily! Thank you for blessing me with this effortlessly ingestible digestible content.

      Do you know something about that? is it coming? is going to progress?A friend of mine told me but I didn't understand very well
      if you know something about this, can you share with me?
      THANK YOU!! This saved my butt today, I’m immensely grateful.


      Obrigado,
      merry

      Delete
  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
  13. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work.
    hotmail login | red ball

    ReplyDelete
  14. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more .
    java training in chennai

    ReplyDelete
  15. You have done really great job. Your blog is very unique and informative. Thanks. Devops Online Training | Data Science Online Training

    ReplyDelete
  16. Nice sharing. R is a language and environment for statistical computing and graphics. Want to make a career in R Programming. Learn R Programming Online Training course @ GangBoard. We are the best provider of online training on evergreen technologies.

    ReplyDelete
  17. Excellent sharing. Testing is a wonderful field for IT peoples. Want to learn Selenium Training reach GangBoard. Hadoop Online Training

    ReplyDelete
  18. Great...You have clearly explained Selenium Training in Chennai about Memorandum of Understanding..It's more informative and easy to understand..Keep on blogging..
    Selenium Training Course in Chennai

    ReplyDelete
  19. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete
  20. Thank you for this valuable information. I have got some important suggestions from it. I'm working in Brave Technologies Private Limited. We provides lowest price of ERP Software for our clients. Contact us on info@bravetechnologies.in
    Manufacturing ERP software Chennai | Automotive ERP in Chennai

    ReplyDelete
  21. Interesting article.... Thanks for sharing this details....
    HTML training in chennai

    ReplyDelete
  22. Thank you for this valuable information. I have got some important suggestions from it. I'm working in Brave Technologies Private Limited. We provides lowest price of ERP Software for our clients. Contact us on info@bravetechnologies.in erp in chennai

    ReplyDelete
  23. This comment has been removed by the author.

    ReplyDelete
  24. Thank you for sharing this information about Java.This Information really useful
    Java Training in Chennai

    ReplyDelete
  25. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
    Java Training in Chennai

    ReplyDelete
  26. Nice Blog.Thank you for Sharing. We are leading erp software software solution providers in chennai.
    erp providers in chennai | cloud erp providers in chennai

    ReplyDelete
  27. you have done a great job. your blog is very informative and unique. iam very happy to read your blogs. Thanks for sharing. If you want to learn software testing in chennai. we Besant Technologies are the best training institute in Chennai.


    Software Testing Training in Chennai

    ReplyDelete
  28. Nice post, Thanks for sharing your information, it is very useful for me. keep rocks.
    Java Training in velachery |Java Training institute in velachery

    ReplyDelete
  29. Amazing, thanks a lot my friend, I was also siting like a your banner image when I was thrown into Selenium.When I started learning then I understood it has got really cool stuff.
    I can vouch webdriver has proved the best feature in Selenium framework.
    Thanks a lot for taking a time to share a wonderful article.


    Selenium Training in Velachery | software testing

    ReplyDelete
  30. Excellent sharing. Testing is a wonderful field for IT peoples. Want to build your website

    White Label Website Builder

    ReplyDelete
  31. I found a lot of interesting information here. A really good post, very thankful and hopeful that you will write many more posts like this one.

    Webdesign Lüdenscheid

    ReplyDelete
  32. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    Java Training in Bangalore

    ReplyDelete
  33. Really cool post, highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you for this rare info!

    selenium training in chennai|
    selenium training in bangalore|

    ReplyDelete
  34. This comment has been removed by the author.

    ReplyDelete
  35. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.
    big data training in bangalore
    big data training in chennai

    ReplyDelete
  36. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!

    www.besanttechnologies.com/training-courses/java-training
    www.trainingbangalore.in/java-training-in-bangalore.html
    www.besanttechnologies.com/training-courses/java-training-in-bangalore

    ReplyDelete
  37. My rather long internet look up has at the end of the day been compensated with pleasant insight to talk about with my family and friends."Devops Training in Bangalore"

    ReplyDelete
  38. It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.

    Data Science Training in Bangalore


    DataScience Training in Chennai



    ReplyDelete
  39. Excellent sharing. Testing is a wonderful field for IT peoples. Want to build your website.


    java Training in Bangalore


    ReplyDelete
  40. I ‘d mention that most of us visitors are endowed to exist in a fabulous place with very many wonderful individuals with very helpful things.

    Devops Training in Bangalore

    ReplyDelete
  41. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.

    java training in bangalore

    ReplyDelete
  42. Thanks a lot very much for the high quality and results-oriented help.
    I won’t think twice to endorse your blog post to anybody who wants
    and needs support about this area.


    java training in chennai


    java training in bangalore

    ReplyDelete
  43. Those guidelines additionally worked to become a good way to
    recognize that other people online have the identical fervor like mine
    to grasp great deal more around this condition.

    I have one doubt why java become more popular then all language

    java training in chennai


    java training in bangalore

    ReplyDelete
  44. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.

    Selenium Training in Chennai

    ReplyDelete
  45. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.

    software testing course in chennai

    ReplyDelete
  46. it is ver valueable for us...for more info visit link below-
    http://www.ducatindia.com/javatraining/

    ReplyDelete
  47. Great job!!! We really appreciate your aspirations and efforts to make ur dreams true. If all of us start thinking about our nation and our people before thinking about ourselves, our country would be leading whole world...
    pest control
    cctv camera
    termite control
    PC Optimization
    windows Installation
    Data Recovery

    ReplyDelete
  48. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site. Selenium Training In Bangalore | Best Selenium Training in Bangalore

    ReplyDelete
  49. Appreciating the persistence you put into your blog and detailed information you provide.
    Data Science Training in Hyderabad

    ReplyDelete
  50. You rock particularly for the high caliber and results-arranged offer assistance. I won't reconsider to embrace your blog entry to anyone who needs and needs bolster about this region.safety course in chennai

    ReplyDelete
  51. Your good knowledge and kindness in playing with all the pieces were
    very useful. I don’t know what I would have done if I had not
    encountered such a step like this.

    Digital Marketing Course In Chennai
    Digital Marketing Training In Chennai
    Seo Training in Chennai

    ReplyDelete
  52. Great post! Thanks for sharing this valuable information.

    Selenium Training in Chennai

    ReplyDelete
  53. Nice post.Thank you so much for sharing.Yiioverflow is a web development company.We have well expert team in Angular JS, Ionic, Yii Framework, Node JS, Laravel, PHP, MySQL, and WordPress.If you want a developer visit.. https://yiioverflow.com/

    ReplyDelete
  54. Great post. The article is too good. Thanks for sharing.

    Java training in Chennai

    ReplyDelete
  55. This was an nice and amazing and the given contents were very useful and the precision has given here is good.
    RPA Training in Chennai

    ReplyDelete

  56. This was an nice and amazing and the given contents were very useful and the precision has given here is good.
    RPA Training in Chennai

    ReplyDelete
  57. Quite interesting post,Thanks for sharing the information.Keep updating good stuff...
    sap abap programming training

    ReplyDelete
  58. Thanks for giving a great information about DevOps Good Explination nice Article
    anyone want to learn advance devops tools or devops online training visit:
    DevOps Online Training contact Us: 9704455959

    ReplyDelete
  59. Thanks for an article.I got a great knowledge.Its very useful.Thanks for sharing DevOps Training In Chennai | DevOps Training Institute In Chennai

    ReplyDelete
  60. I am happy to find this post Very useful for me, as it contains lot of information. I Always prefer to read The Quality and glad I found this thing in you post.

    selenium training in chennai

    ReplyDelete
  61. Thanks for giving a great information about interview-question Good Explination nice Article
    anyone want to learn advance devops tools or devops online training
    DevOps Online Training contact Us: 9704455959

    ReplyDelete
  62. Our digital marketing course in Chennai is targeted at those who are desirous of taking advantage of career opportunities in digital marketing. Marketing professionals who are presently using traditional marketing to meet their business objectives. Start-ups, Entrepreneurs, Business Owners who desire to make use of online media to improve their business goals and objectives.

    Digital Marketing Course in Chennai
    Digital Marketing Training in Chennai
    Online Digital Marketing Training

    ReplyDelete