Serialization and Deserialization in Java - Study24x7
Social learning Network

Serialization and Deserialization in Java

Updated on 15 February 2020
study24x7
javalearning
7 min read 52 views
Updated on 15 February 2020

Serialization is changing the state of Object into binary stream or byte stream. Deserialization is the reverse process where byte stream changes into java object.


The byte stream created is platform-independent. So, the object serialized on one platform can be deserialized on a different platform.

To make a Java object serializable we implement the java.io.Serializable interface.


java.io.Serializable interface


Serializable is a marker interface (has no data member and method). It is used to "mark" Java classes so that the objects of these classes may get a certain capability. The Cloneable and Remote are also marker interfaces.

It must be implemented by the class whose object you want to persist.

The String class and all the wrapper classes implement the java.io.Serializable interface by default.


Advantages of Serialization


  • To save/persist state of an object.
  • To travel an object across a network.


A Serializable class can declare its own UID explicitly by declaring a field name.It must be static, final and of type long.


Example


  1. /**
  2.  * @author javalearning
  3.  */
  4. class Test implements java.io.Serializable {
  5. public int a;
  6. public String b;
  7. /**
  8. * Constructor
  9. * @param a
  10. * @param b
  11. */
  12. public Test(int a, String b) 
  13. this.a = a; 
  14. this.b = b; 
  15. }
  16. }
  17. /**
  18.  * @author javalearning
  19.  */
  20. class Main {
  21. public static void main(String[] args) {
  22. Test test = new Test(1, "Hello world");
  23. String filename = "xyz.ser";
  24. /**
  25. * Serialization
  26. */
  27. try {
  28. /**
  29. * Saving of object in a file
  30. */
  31. FileOutputStream file = new FileOutputStream(filename);
  32. ObjectOutputStream out = new ObjectOutputStream(file);
  33. /**
  34. * Method for serialization of object
  35. */
  36. out.writeObject(test);
  37. out.close();
  38. file.close();
  39. System.out.println("Object serialized");
  40. }
  41. catch (IOException ex) {
  42. System.out.println("IOException");
  43. }
  44. Test test1 = null;
  45. /**
  46. * Deserialization
  47. */
  48. try {
  49. /**
  50. * Reading the object from a file
  51. */
  52. FileInputStream file = new FileInputStream(filename);
  53. ObjectInputStream in = new ObjectInputStream(file);
  54. /**
  55. * Method for deserialization of object
  56. */
  57. test1 = (test) in.readObject();
  58. in.close();
  59. file.close();
  60. System.out.println("Object has been deserialized ");
  61. System.out.println("a: " + test1.a);
  62. System.out.println("b: " + test1.b);
  63. }
  64. catch (IOException ex) {
  65. System.out.println("IOException");
  66. }
  67. catch (ClassNotFoundException ex) {
  68. System.out.println("ClassNotFoundException");
  69. }
  70. }
  71. }


ObjectInputStream class


An ObjectInputStream deserializes objects and primitive data written using an ObjectOutputStream.




study24x7
Write a comment...