Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is serialization still useful?

 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like at some point in the future that serialization will be deprecated.

Somebody told me that since serialization is binary and once you store it to persistent storage or if you're talking to another computer running Java with different versions of objects of the same class (such as using RMI), that this is problematic since once you start doing these types of things that it becomes difficult to add instance variables to your objects.

Doesn't this violate encapsulation?

I don't remember where I heard or read this, but there is some speculation that serialization as we know it now (to a binary format) should be replaced with serialization to XML files which are more well-suited to handling version differences.

Is there any truth to these rumors?

Kaydell
[ June 03, 2007: Message edited by: Barry Gaunt ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I know serialization is useful specially when you need to transfer
objects over the network so that you could reconstruct an object on the
another machine. Serialization is for storing object state to the persistent
storage for later use.

I see in various web applications, when it comes that you need to send an
object of particular class over the network, your class must be serializable.


Not sure what you say, that XML will replace binary transformation of object. As I know in Hibernate technology it happens.

Let's see what others say!


Thanks,
[ June 03, 2007: Message edited by: Chandra Bhatt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is beyond the scope of SCJP. Moving to Java In General (Intermediate).
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't this violate encapsulation?


Interesting point. I suppose it does to some degree. Having the requirement that the receiver must have the same version of the class as the sender adds version information to the public knowledge about the object.

When you start going beyond your JVM, say to a persistance tool or passing objects over a network, you can introduce a lot of details with that potential. The boundaries between the ideal object model and the non-OO world are always a bit messy.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, you do not need to have the same class file on each end; you need to have compatible class files on each end. Serialization includes a versioning mechanism that allows you to modify your classes as needed and still be able to read old serialized data files. Google "serialversionuid".

JAXB is the most "official" way to do serialization to XML; there are loads of commercial and non-commercial tools that do it, too. XML serialization is handy for long-term storage and communication between Java and non-Java code. For communicating over a wire between Java programs, though, a binary wire format is always going to be most efficient. Whether its JRMP with serialization, or IIOP, or something else, there's a niche for it, and it's a different niche than is served by serialization to XML.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you say, that XML will replace binary transformation of object. As I know in Hibernate technology it happens.


That's what the java.beans.XMLEncoder/XMLDecoder classes are for. But they only work for objects that conform to the JavaBeans specification.

Hibernate does not use XML to store objects.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hibernate does not use XML to store objects.

Thanks Ulf!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic