• Post Reply Bookmark Topic Watch Topic
  • New Topic

What are the advantages of serialization?  RSS feed

 
Adeiza Yusuf
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just read about object serialization in java and seem to understand it a little bit but am sure if can know any real world advantage it would give me more and better understanding. please could any one tell me one or more advantage and use of Object serialization in java.

Thank you.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization is useful because it's easy. Just implement the Serializable interface and you're good to go (off course there are some pitfalls). You can serialize configurations, small data structures and more.
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization makes it easy to write the class to a stream. I'm using it to read / write class data to binary files.
 
Adeiza Yusuf
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I appreciate your responses.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization indeed makes it easy to store the state of objects in a file (or somewhere else). But you also have to be aware of the disadvantages of serialization. A major disadvantage is that the content of the file where you serialize an object in will be tightly coupled to your source code. If you change something in the source code of the class, then you can't read serialized objects that were stored with the old version of the class anymore.

That problem becomes a nightmare when you are writing software that's used by customers. Suppose that you publish version 1.1 of your program, and people have files saved with version 1.0. Now version 1.1 can't read those files anymore because you've changed the class. You also don't have any control over the exact file format.

Serialization is suited for short-term or temporary storage (for example for storing things in a cache, that can be discarded at anytime), or for transferring objects over a network connection. But it's not very well suited for long-term storage. You'd better use a well-documented and standard file format for long-term storage of data.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:That problem becomes a nightmare when you are writing software that's used by customers. Suppose that you publish version 1.1 of your program, and people have files saved with version 1.0. Now version 1.1 can't read those files anymore because you've changed the class. You also don't have any control over the exact file format.

That's not completely true. Even without handling all serialization manually there are a few tricks that give you a bit more control. But in the end it is true that adding, removing or changing non-transient fields will give you a lot more work to avoid invalidating older saved data.

But it's not very well suited for long-term storage. You'd better use a well-documented and standard file format for long-term storage of data.

I wouldn't say it's not suited, but there are indeed better alternatives. Simple properties files could be used, especially in combination with java.util.Properties. XML is also usually a good choice.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!