Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is object serialization?

What is meant by "object serialization"? Can you please explain it with some examples?

like image 536
Warrior Avatar asked Jan 15 '09 18:01

Warrior


People also ask

What is object serialization why it is needed?

Serialization in Java allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage. Deserialization is the process of converting Object stream to actual Java Object to be used in our program.

What is meant by object serialization and deserialization?

Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.

What is object serialization in JavaScript?

The process whereby an object or data structure is translated into a format suitable for transfer over a network, or storage (e.g. in an array buffer or file format). In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON. stringify() .

What is the advantage of object serialization in Java?

Serialization allows us to transfer objects through a network by converting it into a byte stream. It also helps in preserving the state of the object. Deserialization requires less time to create an object than an actual object created from a class.


2 Answers

Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialized - converted into a replica of the original object.

like image 123
TarkaDaal Avatar answered Oct 10 '22 07:10

TarkaDaal


You can think of serialization as the process of converting an object instance into a sequence of bytes (which may be binary or not depending on the implementation).

It is very useful when you want to transmit one object data across the network, for instance from one JVM to another.

In Java, the serialization mechanism is built into the platform, but you need to implement the Serializable interface to make an object serializable.

You can also prevent some data in your object from being serialized by marking the attribute as transient.

Finally you can override the default mechanism, and provide your own; this may be suitable in some special cases. To do this, you use one of the hidden features in java.

It is important to notice that what gets serialized is the "value" of the object, or the contents, and not the class definition. Thus methods are not serialized.

Here is a very basic sample with comments to facilitate its reading:

import java.io.*; import java.util.*;  // This class implements "Serializable" to let the system know // it's ok to do it. You as programmer are aware of that. public class SerializationSample implements Serializable {      // These attributes conform the "value" of the object.      // These two will be serialized;     private String aString = "The value of that string";     private int    someInteger = 0;      // But this won't since it is marked as transient.     private transient List<File> unInterestingLongLongList;      // Main method to test.     public static void main( String [] args ) throws IOException  {           // Create a sample object, that contains the default values.         SerializationSample instance = new SerializationSample();          // The "ObjectOutputStream" class has the default          // definition to serialize an object.         ObjectOutputStream oos = new ObjectOutputStream(                                 // By using "FileOutputStream" we will                                 // Write it to a File in the file system                                // It could have been a Socket to another                                 // machine, a database, an in memory array, etc.                                new FileOutputStream(new File("o.ser")));          // do the magic           oos.writeObject( instance );         // close the writing.         oos.close();     } } 

When we run this program, the file "o.ser" is created and we can see what happened behind.

If we change the value of: someInteger to, for example Integer.MAX_VALUE, we may compare the output to see what the difference is.

Here's a screenshot showing precisely that difference:

alt text

Can you spot the differences? ;)

There is an additional relevant field in Java serialization: The serialversionUID but I guess this is already too long to cover it.

like image 40
OscarRyz Avatar answered Oct 10 '22 07:10

OscarRyz