• 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 in java

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please explain serialization in layman terms.

when a program is exexuted, its just executed on the server and result displayed on the screen on browser. It just takes a second to execute this program. So all the objects in the program get executed in a second. Where does serilization come in picture ? How can we preserve state of the object in program being executed ? We dont do any serailization. I am completely confused with this.

Is there any practical or day to day use example where I can see use of serialization ?

thanks
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, let's stick with the Web Application idea. Suppose your web app is a shopping cart for a store. Your customer comes in and puts several things in their cart. They don't buy their stuff, and they leave. Wouldn't it be nice if when they came back they could go to their shopping cart and continue exactly where they left off? Well, you could store that information in memory the entire time but you would run out of memory if you have more than a few customers. So instead you save the shopping cart to more permanent storage (a file or database) when the customer leaves - you use serialization. And when they come back you deserialize it and they never have to know the difference.

There are a lot of reasons for serializing data - long term storage, persistence of settings or dynamic data, transport or communication from client to server...
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

To understand serialization you may refer Keithy Sierra Head First Java. It would be really helpful to understand this concept.

Regards,
Patricia.

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serialization in day today life:---

The data on your system's hard disk. The songs in your IPod. Songs and videos you download from different websites.
These are serialized as you don't want to loose them.

So any data which you want to save(persists) has to be serialized. This is very layman definition.
Hope i helped you!!!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"VivekSingh SinghVivek", please read your private messages.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still I am confused as before.

This is what I read from a book.
"Serializing the object is the process of writing its data members to disk. Deserializing an object is the process of reconstructing the object instance from the data members written to disk."

I guess this is where I am confused.

I dont understand when serialization takes place

1) Is serialization something that takes place after code is executed ? while downloading or uploading songs, where does java code come in picture ? If songs or videos are on the server, then are they there in the form of java objects ? where is java used in keeping songs on server ? When we send a request to download a particular video , does it go the java program (If java is used) and then that program retrieves that video from database and these videos are in serialized form ?

2) when a java byte code "serializationTest" is executed, whatever code is there inside this class gets executed and output is displayed on the screen.
Once the output is displayed we can save this output in any file by taking screen shot or downloading it
3) Once the bytecode is executed, all the objects have been executed and everything we can save is output. What is the state of objects after a code is executed ? where does the question of preserving objects arise once code is executed ? Do we preserve the state of objects while code is being executed ?

5) As Vivek Singh explained, downloaded songs , videos etc are the serialized, so we wont loose them. What does that mean ? Are the songs and videos in the serialized form ? If they are not serialized, why and how would we loose them while downliading ? When I am downloading a video, I am not using any javaclass or anything like that to deserialize them, so how does serialized objects become deserialized ? When I am uploading a video , I dont do any serialization. Then how do these videos become serialized ?
6) Does serialization save space ? Many times Songs and videos are zip files, so is serialization and file compression same thing ?
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nirjari patel wrote:Still I am confused as before.

This is what I read from a book.
"Serializing the object is the process of writing its data members to disk. Deserializing an object is the process of reconstructing the object instance from the data members written to disk."

I guess this is where I am confused.

I dont understand when serialization takes place

1) Is serialization something that takes place after code is executed ?



It happens IN CODE, just like any other method call. Serialization is a process of writing a Java Object to a Stream. It takes code to do that (You use an ObjectOutputStream, call its writeObject() method and pass it the Object to serialize. It then does the appropriate work for your Serializable Object).

while downloading or uploading songs, where does java code come in picture ? If songs or videos are on the server, then are they there in the form of java objects ? where is java used in keeping songs on server ? When we send a request to download a particular video , does it go the java program (If java is used) and then that program retrieves that video from database and these videos are in serialized form ?



The music and video correlation is a bad one, Music and Video are data, not Java Objects. Java Objects may be used to model the music or video file but I think it is a bad scenario to dwell on. Serialization is about storing the data in a Java Object. If you use Java, then you are familiar with Objects. Pick one of your favorites. Objects have STATE, that is, values that describe the Object at this particular moment in time (if it is a car, maybe it has Color, gas level, and number of occupants, for example). That STATE of the Object exists only inside the current Java application (the current JVM). Other applications, or machines can't see it. If the Java program ends, that information goes away.

When you serialize an object you write that STATE to a file (you persist it) so it exists outside of the JVM. This means if the Java Application ends, then when it starts up again, you can read that information in and run from the exact STATE you left from. Or you can send that file to some other Java Application, and the other Java Application will know the STATE of the Object (what color the car is, how many people are in it, how much gas it has...), and so that other Java Application can use the Object (car) as well.


2) when a java byte code "serializationTest" is executed, whatever code is there inside this class gets executed and output is displayed on the screen.
Once the output is displayed we can save this output in any file by taking screen shot or downloading it



Serialization is not about saving the OUTPUT of an application, it is about saving the STATE of an Object. If you want to save the OUTPUT then do that using File IO, or one of any other number of methods. If you want to store the STATE of an Object, so that Object can be re-used as is in other JVMs, use Serialization.


3) Once the bytecode is executed, all the objects have been executed



Code is executed. Sometimes code is executed on Objects. But I don't really know what an Object being executed means ...


and everything we can save is output. What is the state of objects after a code is executed ?



Whatever STATE the code leaves the Object in. There is no general answer to this. It depends entirely on the code you are talking about and the Object you are talking about.

where does the question of preserving objects arise once code is executed ?



Preserving Objects depends entirely on how you want to use them, and nothing to do with when code gets executed. I am not sure I understand the question.

Do we preserve the state of objects while code is being executed ?



Yes. The code used to serialize the Object must be executing in order to serialize the Object... Other code which works on the Object usually should not be running during serialization. It is usually best to prevent access to an Object while it is being serialized. You are free to run other code which does not use the Object, and you are free to use the Object before and after serialization though...


5) As Vivek Singh explained, downloaded songs , videos etc are the serialized, so we wont loose them. What does that mean ? Are the songs and videos in the serialized form ? If they are not serialized, why and how would we loose them while downliading ? When I am downloading a video, I am not using any javaclass or anything like that to deserialize them, so how does serialized objects become deserialized ? When I am uploading a video , I dont do any serialization. Then how do these videos become serialized ?



Right, movies and songs really have nothing to do with serialization. You should forget about that. Typically a song or movie file is not some serialized object, it is a data format unto itself, and has nothing to do Java or Serialization.

6) Does serialization save space ? Many times Songs and videos are zip files, so is serialization and file compression same thing ?



It has nothing to do with compression. Serialized Objects may be compressed but they don't have to be (just like a text file may be compressed but doesn't have to be). Serialization is not about saving space on a disk. Sometimes it is used to move Object out of your Java Application's memory and store it on disk until some later point when it is needed again. When used like this it is used to save memory but not (disk) space (rather the opposite, really, you consume disk space).

I think you may be under the impression that Serialization is done on the Java bytecode, and that is is some process used to manage/modify/work with bytecode. It is not. Serialization is performed on the Runtime Object, not its code. It persists the Runtime values in the fields of the Object in a fashion that allows them to exist outside the Java Application.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By preserving the state of the object I understand that, object is stored in a file on the system. It can be hard disk or RAM. And when we want this object back, we can just get it from this file.

Now question is , When an object is serialized, a file is created , Does this file have the same name as the name of the object ? How to recall this serialized object (file) ? Do we have to provide complete path to get recall this file ?

Please correct me, if my understanding is wrong.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The file can have any name you give it.

There isn't a lot in the Java™ Tutorials, and I am not convinced what there is is very helpful to you That link gives you this link to the serialization page. Again, it might not be very useful, but have a look. This "system architecture" link (this time for Java6, not "Java7") might be a bit more useful.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic