title: How does Java's serialization work and when it should be used instead of some other persistence technique?
body: <p>I've been lately trying to learn more and generally test Java's serialization for both work and personal projects and I must say that the more I know about it, the less I like it. This may be caused by misinformation though so that's why I'm asking these two things from you all:</p>
  
  <p><strong>1:</strong> On byte level, how does serialization know how to match serialized values with some class?</p>
  
  <p>One of my problems right here is that I made a small test with ArrayList containing values "one", "two", "three". After serialization the byte array took 78 bytes which seems awfully lot for such low amount of information(19+3+3+4 bytes). Granted there's bound to be some overhead but this leads to my second question:</p>
  
  <p><strong>2:</strong> Can serialization be considered a good method for persisting objects at all? Now obviously if I'd use some homemade XML format the persistence data would be something like this</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;object&gt;
      &lt;class="java.util.ArrayList"&gt;
      &lt;!-- Object array inside Arraylist is called elementData --&gt;
      &lt;field name="elementData"&gt;
          &lt;value&gt;One&lt;/value&gt;
          &lt;value&gt;Two&lt;/value&gt;
          &lt;value&gt;Three&lt;/value&gt;
      &lt;/field&gt;
  &lt;/object&gt;
  </code></pre>
  
  <p>which, like XML in general, is a bit bloated and takes 138 bytes(without whitespaces, that is). The same in JSON could be </p>
  
  <pre class="lang-json prettyprint-override"><code>{
      "java.util.ArrayList": {
          "elementData": [
              "one",
              "two",
              "three"
          ]
      }
  }
  </code></pre>
  
  <p>which is 75 bytes so already slightly  smaller than Java's serialization. With these text-based formats it's of course obvious that there has to be a way to represent your basic data as text, numbers or any combination of both.</p>
  
  <p>So to recap, how does serialization work on byte/bit level, when it should be used and when it shouldn't be used and what are real benefits of serialization besides that it comes standard in Java?</p>