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.