• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

examples of when to use serialization

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think i understand the basic concept, but at first i didnt. in searching this site i saw others who were a bit confused about this. i have one example.

say you wrote a game and all you need to do, is at the end update the top scores. then just writing name and score to a file is quite simple.
but, suppose instead you want to save the game. now we are talking about hundreds of variables. creating a format to save the file and then have to parse the file and assign the values to the variables would be a nightmare of grunt work, and prone to errors as well.

any other examples would help both me and others who havent had to do this before.
 
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also serialise the game to a byte[] and send across the globe to your opponent across the street.
 
Marshal
Posts: 26907
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that "serialization" is a generic term which refers to converting between an internal (Java objects) format and and external (bytes on the wire or in a file) format.

So don't assume that "serialization" means you have to use ObjectOutputStream and all of that sort of thing. There is also java.beans.XMLEncoder which can be used to serialize an object to XML, for example.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but, suppose instead you want to save the game. now we are talking about hundreds of variables. creating a format to save the file and then have to parse the file and assign the values to the variables would be a nightmare of grunt work, and prone to errors as well.



Or - code a single object "StateOfTheGame" which holds all these variables and is Serializable. No parsing, variables already have the state.

Don't be afraid of Serialize-ing a complex object - it works great and can be quite compact and fast. StateOfTheGame can contain collections and other complex objects as long as they are also Serializable.

Bill
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serialization means converting a Java object that's in memory to bytes (binary data) so that you can store it (to be loaded again later) or to transmit it over the network.

In the project that I'm currently working on, we are building a system that can work in a clustered way - there can be multiple servers in the system that work together. Sometimes these servers need to share data, and we do that by serializing objects containing the data, sending the bytes to the other server over the network, and then deserializing the bytes back into a Java object on the other server.

Note that the standard Java serialization mechanism is not well suited for long-term storage of objects. Don't write a program in which the user's data is stored in files by using Java's built-in serialization mechanism. The problem is that the serialization mechanism very tightly binds your source code to the bytes that are stored in the file - if you change something to your classes in a newer version of your program, then it will not be able to load old files anymore. Also, the exact format of serialized data is not easy to understand (I guess the specifications are available somewhere, but it's not a well-known standard format). For such applications, it's much better to use a standard format (XML for example, or any other standard format).
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic