aspose file tools*
The moose likes Beginning Java and the fly likes Serialization in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Serialization in java" Watch "Serialization in java" New topic
Author

Serialization in java

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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...


Steve
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Hi,

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

Regards,
Patricia.

Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
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!!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

"VivekSingh SinghVivek", please read your private messages.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Apr 23, 2009
Posts: 374
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization in java