• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

serialization instead of cloning?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using Serialization over piped streams I believe I can perform deep copy (cloning).
Is this correct? Here is a code snippet:

Twisting together piped streams in the same thread is prone to deadlock as per javadoc.
But that aside, "cloneObj" is a clone of "originalObj" in 100% everyway, right?
Should this be considered a valid technique in production code? If not, why not?
A senior developer informed me that the fathers of Java intended serialization to only be used to persist an object outside the jvm. Using the twisted pipes technique was wrong because it violates the "intended use" of serialization and he refused to discuss any further.

What says the Java Ranch? Serialization is deep copy. Clone is deep copy. I can serialize to perform cloning. right?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not particularly elegant or efficient, but I've done it sometimes because I had to. If performance doesn't matter much, but simplicity does, then it's a good strategy.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Masa Saito wrote:Serialization is deep copy. Clone is deep copy. I can serialize to perform cloning. right?


By default, no - clone() uses shallow copy. If you want deep copy, using serialization is worth considering. It you don't want to carefully modify the clone() method of each class accordingly, and you haven't yet determined that performance is poor and this is the primary bottleneck, then this is a good way to achieve your objectives, in my opinion.

As for your use of piped streams, it seems like this is an unrelated consideration. On one hand, you can choose between using clone() or using serialization. On another hand, completely independent, you can choose between writing to a piped stream, or a ByteArrayOutputStream, or a file, or something else. The first is a choice of what format to use; the latter is a choice of destination. The first is what to write, and the latter is where to write it. As far as I can see here, the two choices are independent and unrelated.
 
reply
    Bookmark Topic Watch Topic
  • New Topic