What does Serializable mean?
Serialization is persisting an object from memory to a sequence of bits, for instance for saving onto the disk. Deserialization is the opposite - reading data from the disk to hydrate/create an object.
In the context of your question, it is an interface that if implemented in a class, this class can automatically be serialized and deserialized by different serializers.
What is Serializable? What does this mean?
It means that the class can be serialized. This means that it can be converted into an stream of 0's and 1's and be sent to other service, like a webservice or and ORM (hibernate) or whatever. Then this service knows how store the stream.
Also, your program can receive a serialized class and create an instance for it from the stream of 0's and 1's.
It's the way to "save" instances of classes and "restore" them at any other moment in time.
More info at
- http://download.oracle.com/javase/6/docs/api/java/io/Serializable.html
- http://en.wikipedia.org/wiki/Serialization
To be able to make a class serializable, you must implement the interface "Serializable".
Why so? Because when the moment to serialize a class arrive, the function
writeObtject
will be called to convert the object into bytes. On the other hand, when you have the bytes and you want to get the instance of the class, the functionreadObject
will be called.Why is not automatic? A variable value it's already represented by bytes. Because the problem comes for example when a class holds instances of other classes. What do you do? You serialize the other classes as well? You just keep the reference address? It's pretty complex and it may be dependant on your type of project and design.
What is Serializable in Java?
serializable is a special interface that specifies that class is serialiazable. It's special in that unlike a normal interface it does not define any methods that must be implemented: it is simply marking the class as serializable. For more info see the Java docs.
As to what "serializable" means it simply means converting an instance of a class (an object) into a format where it can be written to disk, or possibly transmitted over a network. You could for example save your object to disk and reload it later, with all the field values and internal state saved. See the wikipedia page for more info.
What is [Serializable] and when should I use it?
What is it?
When you create an object in a .Net framework application, you don't need to think about how the data is stored in memory. Because the .Net Framework takes care of that for you. However, if you want to store the contents of an object to a file, send an object to another process or transmit it across the network, you do have to think about how the object is represented because you will need to convert to a different format. This conversion is called SERIALIZATION.
Uses for Serialization
Serialization allows the developer to save the state of an object and recreate it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions like sending the object to a remote application by means of a Web Service, passing an object from one domain to another, passing an object through a firewall as an XML string, or maintaining security or user-specific information across applications.
Apply SerializableAttribute
to a type to indicate that instances of this type can be serialized. Apply the SerializableAttribute
even if the class also implements the ISerializable
interface to control the serialization process.
All the public and private fields in a type that are marked by the SerializableAttribute
are serialized by default, unless the type implements the ISerializable
interface to override the serialization process. The default serialization process excludes fields that are marked with NonSerializedAttribute
. If a field of a serializable type contains a pointer, a handle, or some other data structure that is specific to a particular environment, and cannot be meaningfully reconstituted in a different environment, then you might want to apply NonSerializedAttribute
to that field.
See MSDN for more details.
Edit 1
Any reason to not mark something as serializable
When transferring or saving data, you need to send or save only the required data. So there will be less transfer delays and storage issues. So you can opt out unnecessary chunk of data when serializing.
When should we implement Serializable interface?
From What's this "serialization" thing all about?:
It lets you take an object or group of
objects, put them on a disk or send
them through a wire or wireless
transport mechanism, then later,
perhaps on another computer, reverse
the process: resurrect the original
object(s). The basic mechanisms are to
flatten object(s) into a
one-dimensional stream of bits, and to
turn that stream of bits back into the
original object(s).Like the Transporter on Star Trek,
it's all about taking something
complicated and turning it into a flat
sequence of 1s and 0s, then taking
that sequence of 1s and 0s (possibly
at another place, possibly at another
time) and reconstructing the original
complicated "something."So, implement the
Serializable
interface when you need to store a copy of the object, send them to another process which runs on the same system or over the network.Because you want to store or send an object.
It makes storing and sending objects easy. It has nothing to do with security.
What is the meaning of serialization in programming languages?
Say you have two applications that run on two different physical machines. Both of the applications need to exchange data that is commonly used by both applications. These application talk to each other to share the data with some mediums, these mediums could be a file-system, tcp or udp connections or any other suitable network protocol or may be direct in-memory data exchange. Any of these mediums would only understand data that is described in the form of a series of bits. So when one application needs to send a value 10 to another, the value 10 would be sent as its binary representation 1010 and you would also pass some information that describes 1010. This meta information will also be a series of bits that the other application can easily understand. That was easy though.
Lets take another example, wherein these two apps need to exchange a more complex, non primitive data-type. Lets say they need to exchange the objects of type Book where Book is a custom defined class in your application and both the applications have the definition of type Book.
public class Book
{
Book() { }
public long BookId { get;set; }
public string Author { get;set; }
public string Title { get;set; }
}
How will you exchange the objects of type book between the two applications? To be able to share the object between two apps, you will need to be able to convert the Book objects into binary representation. This is where serialization comes into the picture.
With the help of Serialization you can define how an object can be converted into its binary representation. The receiving application would do the reverse process, that is De-Serialization, that constructs a Book object from its binary representation.
What is object serialization?
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.
Related Topics
Platform Independent Paths in Java
How to Compile & Run Java Program in Another Java Program
Best Way to Encode Text Data for Xml in Java
Java Ioexception "Too Many Open Files"
Layering Multiple Glasspane's in a Root Container
Jprogressbar Isn't Progressing
Maven Parent Pom VS Modules Pom
How to Convert a String to Double in Java Using a Specific Locale
Randomly Select an Item from a List
Spring Boot 2.0.X Disable Security for Certain Profile
Java 8 Date and Time: Parse Iso 8601 String Without Colon in Offset
How to Create Sparksession with Hive Support (Fails with "Hive Classes Are Not Found")
Java Streams: Replacing Groupingby and Reducing by Tomap