{"id":210,"date":"2025-06-24T06:59:31","date_gmt":"2025-06-24T06:59:31","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=210"},"modified":"2025-07-16T10:28:34","modified_gmt":"2025-07-16T10:28:34","slug":"what-is-serialization-and-deserialization","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/what-is-serialization-and-deserialization\/","title":{"rendered":"What is Serialization and Deserialization?"},"content":{"rendered":"\n<p><code>In Java (and many other programming languages), serialization and deserialization are techniques used to convert objects into a format that can be easily saved or transferred\u2014and then later restored.<\/code><\/p>\n\n\n\n<p>\ud83d\udce6 <strong>Serialization<\/strong><\/p>\n\n\n\n<p>Serialization is the process of converting a Java object into a <em>stream of bytes<\/em>. This is useful when you want to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Save an object to a file<\/li>\n\n\n\n<li>Send an object over a network<\/li>\n\n\n\n<li>Store an object in a database<\/li>\n<\/ul>\n\n\n\n<p>Once it\u2019s serialized, it can be stored or transferred easily.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(\"data.ser\"));<br>out.writeObject(myObject); \/\/ Converts the object to bytes<br>out.close();<\/code><\/pre>\n\n\n\n<p>\u26a0\ufe0f The class must implement <code>Serializable<\/code> to be eligible for serialization:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"702\" height=\"208\" src=\"https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-3.png\" alt=\"\" class=\"wp-image-211\" srcset=\"https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-3.png 702w, https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-3-300x89.png 300w\" sizes=\"auto, (max-width: 702px) 100vw, 702px\" \/><\/figure>\n\n\n\n<p>class MyData implements Serializable { int id; String name; }<\/p>\n\n\n\n<p>\ud83d\udcbe <strong>Deserialization<\/strong><\/p>\n\n\n\n<p>Deserialization is the reverse\u2014it reconstructs the object from its byte-stream form.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ObjectInputStream in = new ObjectInputStream(new FileInputStream(\"data.ser\")); MyData obj = (MyData) in.readObject(); \/\/ Recreates the object \nin.close();<\/code><\/pre>\n\n\n\n<p>\ud83d\udee1 Pro Tip: If a class changes (e.g., new fields are added) after serialization, you may get compatibility issues. That\u2019s where a <code>serialVersionUID<\/code> can help maintain version control.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn what serialization and deserialization mean in Java. Discover how objects are converted to byte streams and restored for storage, transfer, or reuse.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[17],"tags":[],"class_list":["post-210","post","type-post","status-publish","format-standard","hentry","category-java-interview-questions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/comments?post=210"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":456,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/210\/revisions\/456"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}