What is the purpose of Serialization in Java?

前端 未结 8 2346
花落未央
花落未央 2020-11-29 15:29

I have read quite a number of articles on Serialization and how it is so nice and great but none of the arguments were convincing enough. I am wondering if someone can reall

相关标签:
8条回答
  • 2020-11-29 15:57

    Let's define serialization first, then we can talk about why it's so useful.

    Serialization is simply turning an existing object into a byte array. This byte array represents the class of the object, the version of the object, and the internal state of the object. This byte array can then be used between JVM's running the same code to transmit/read the object.

    Why would we want to do this?

    There are several reasons:

    • Communication: If you have two machines that are running the same code, and they need to communicate, an easy way is for one machine to build an object with information that it would like to transmit, and then serialize that object to the other machine. It's not the best method for communication, but it gets the job done.

    • Persistence: If you want to store the state of a particular operation in a database, it can be easily serialized to a byte array, and stored in the database for later retrieval.

    • Deep Copy: If you need an exact replica of an Object, and don't want to go to the trouble of writing your own specialized clone() class, simply serializing the object to a byte array, and then de-serializing it to another object achieves this goal.

    • Caching: Really just an application of the above, but sometimes an object takes 10 minutes to build, but would only take 10 seconds to de-serialize. So, rather than hold onto the giant object in memory, just cache it out to a file via serialization, and read it in later when it's needed.

    • Cross JVM Synchronization: Serialization works across different JVMs that may be running on different architectures.

    0 讨论(0)
  • 2020-11-29 16:03

    I can share my story and I hope it will give some ideas why serialization is necessary. However, the answers to your question are already remarkably detail.

    I had several projects that need to load and read a bunch of text files. The files contained stop words, biomedical verbs, biomedical abbreviations, words semantically connected to each other, etc. The contents of these files are simple: words!

    Now for each project, I needed to read the words from each of these files and put them into different arrays; as the contents of the file never changed, it became a common, however redundant, task after the first project.

    So, what I did is that I created one object to read each of these files and to populate individual arrays (instance variables of the objects). Then I serialized the objects and then for the later projects, I simply deserialized them. I didn't have to read the files and populate the arrays again and again.

    0 讨论(0)
  • 2020-11-29 16:03

    The most obvious is that you can transmit the serialized class over a network, and the recepient can construct a duplicate of the original instanstance. Likewise, you can save a serialized structure to a file system.

    Also, note that serialization is recursive, so you can serialize an entire heterogenous data structure in one swell foop, if desired.

    0 讨论(0)
  • 2020-11-29 16:05

    I use serialized objects to standardize the arguments I pass to functions or class constructors. Passing one serialized bean is much cleaner than a long list of arguments. The result is code that is easier to read and debug.

    0 讨论(0)
  • 2020-11-29 16:09

    While you're running your application, all of its objects are stored in memory (RAM). When you exit, that memory gets reclaimed by the operating system, and your program essentially 'forgets' everything that happened while it was running. Serialization remedies this by letting your application save objects to disk so it can read them back the next time it starts. If your application is going to provide any way of saving/sharing a previous state, you'll need some form of serialization.

    0 讨论(0)
  • 2020-11-29 16:09

    Serialized objects maintain state in space, they can be transferred over the network, file system, etc... and time, they can outlive the JVM that created them.

    Sometimes this is useful.

    0 讨论(0)
提交回复
热议问题