Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Serializable other than Writing& Reading object to/from File

Tags:

In Which Cases it is a good coding practice to use implements serializable other than Writing & Reading object to/from file.In a project i went through code. A class using implements serializable even if in that class/project no any Writing/Reading objects to/from file?

like image 517
Anurag_BEHS Avatar asked Jul 20 '15 18:07

Anurag_BEHS


People also ask

What can serialization be used for?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

Where is serialization used in real time?

The realtime use of serialization is to save the state of object or we can say persist an object and it's mainly use in networks where we want to travel an object over network.

Why serializable is required?

Serialization is usually used When the need arises to send your data over network or stored in files. By data I mean objects and not text. Now the problem is your Network infrastructure and your Hard disk are hardware components that understand bits and bytes but not JAVA objects.

What objects can be serialized?

To serialize an object means to convert its state to a byte stream so way that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java.


2 Answers

If the object leaves the JVM it was created in, the class should implement Serializable.

Serialization is a method by which an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

This is the main purpose of de-serialization. To get the object information, object type, variable type information from a written(loosely speaking) representation of an object. And hence serialization is required in the first place, to make this possible.

So, whenever, your object has a possibility of leaving the JVM, the program is being executed in, you should make the class, implement Serializable.

Reading/Writing objects into files (Memory), or passing an object over internet or any other type of connection. Whenever the object, leaves the JVM it was created in, it should implement Serializable, so that it can be serialized and deserialized for recognition once it enters back into another/same JVM.

Many good reads at :

  • 1: Why Java needs Serializable interface?
  • 2: What is the purpose of Serialization in Java?
like image 144
Samrat Dutta Avatar answered Sep 22 '22 14:09

Samrat Dutta


Benefits of serialization:

  1. To persist data for future use.

  2. To send data to a remote computer using client/server Java technologies like RMI , socket programming etc.

  3. To flatten an object into array of bytes in memory.

  4. To send objects between the servers in a cluster.

  5. To exchange data between applets and servlets.

  6. To store user session in Web applications

  7. To activate/passivate enterprise java beans.

You can refer to this article for more details.

like image 25
Ravindra babu Avatar answered Sep 21 '22 14:09

Ravindra babu