Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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).</p> <p>It is very useful when you want to transmit one object data across the network, for instance from one JVM to another.</p> <p>In Java, the serialization mechanism is built into the platform, but you need to implement the <em>Serializable</em> interface to make an object serializable.</p> <p>You can also prevent some data in your object from being serialized by marking the attribute as <em>transient</em>.</p> <p>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 <a href="https://stackoverflow.com/questions/15496/hidden-features-of-java/142676#142676">hidden features in java</a>.</p> <p>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.</p> <p>Here is a very basic sample with comments to facilitate its reading:</p> <pre><code>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&lt;File&gt; 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(); } } </code></pre> <p>When we run this program, the file "o.ser" is created and we can see what happened behind.</p> <p>If we change the value of: <strong>someInteger</strong> to, for example <strong>Integer.MAX_VALUE</strong>, we may compare the output to see what the difference is.</p> <p>Here's a screenshot showing precisely that difference:</p> <p><img src="https://i.stack.imgur.com/oIVA5.png" alt="alt text"></p> <p><em><sub>Can you spot the differences? ;)</sub></em></p> <p>There is an additional relevant field in Java serialization: The <a href="http://www.google.com/search?q=serialversionUID" rel="noreferrer">serialversionUID</a> but I guess this is already too long to cover it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload