This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization in java

 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai im having some doubts in this program:



1)why we have used writeInt in line three?what it does and what is the use of this in this program?

2)does the methods writeObject(ObjectOutputStream os) and readObject(ObjectInputStream is) is the altenative of


and


please explain me in detail thanks in advance
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kiruthigha rajan wrote:

1)why we have used writeInt in line three?what it does and what is the use of this in this program?



First, we can only speculate -- as we don't have any context on what is the implementation for.

It looks like programmer don't want to serialize the Collar object -- the comment states that it isn't possible to serialize it.... However, the Dog object needs it. And it looks like it is possible to simply get another Collar upon deserialization -- provided the collar size is correct. So, the programmer sent the dog collar size as part of the serialized stream.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kiruthigha rajan wrote:
2)does the methods writeObject(ObjectOutputStream os) and readObject(ObjectInputStream is) is the altenative of


and



No. This is not an alternative. The first snippet is a modification on how the object should be serialized (specifically the components related to the Dog class only). This set of snippets is the actual serialization and deserialization of the object. This set of code will call the other set of code when it is time to serialize the Dog class component of the Dog object.

Henry

 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between writeObject(),readObject() and defaultWriteObject(),defaultReadObject()?
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sun has a decent article that describes everything.

http://java.sun.com/developer/technicalArticles/Programming/serialization/

It's certainly better than learning it piecemeal via forum exchanges.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic