Skip to content

Instantly share code, notes, and snippets.

@codinko
Created October 16, 2021 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codinko/d7629860a1d37c8933de5d145589c19b to your computer and use it in GitHub Desktop.
Save codinko/d7629860a1d37c8933de5d145589c19b to your computer and use it in GitHub Desktop.
package com.learn.java;
public class Main {
public static void main(String[] args) {
//case-1: serializing a custom class that does not implement serializable interface
////OUTPUT - Got exception while doing serialization... : java.io.NotSerializableException: com.learn.java.Student
//case-2: serializing a custom class that implement serializable interface, but no serialversionUID. Deserialize it . ( if JVM same then it will be success..
// this can fail when client-server model with diff JVM implementation)
// add a new field and try to deserialize the older serialized object. this will fail.
//case-3: serializing a custom class that implement serializable interface, and having serialversionUID. Deserialize it .
// add a new field (private or public) ( dont change the serialVersionUID) and try to deserialize the older serialized object. this will succeed.
//case-4: serializing a custom class that implement serializable interface, and having serialversionUID. Deserialize it .
// add a new field ( and change the serialVersionUID) and try to deserialize the older serialized object. this will FAIL.
//case5: ArrayList of custom class that does not implement serializable
//will it fail or succeed...
//ArrayList of custom class that does not implement serializable , but without serialVersionUID
//Document it..
// case5: Arralist , Hashmap, etc - are they serializable.. yes..they are .. along with String, Integer etc
//Document it. no need of code as you know it..
}
}
----------------------------------------------------
----------1-----------------------------------------
----------------------------------------------------
package com.learn.java;
public class StudentOfCase1 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
-----
*********
package com.learn.java;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
// //case-1: serializing a custom class that does not implement serializable interface
//OUTPUT - Got exception while doing serialization... : java.io.NotSerializableException: com.learn.java.Student
public class SerializeCase1 {
public static void main(String[] args) {
StudentOfCase1 student = new StudentOfCase1();
student.setName("paramu");
try {
FileOutputStream fos = new FileOutputStream("file1.text");
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(student);
out.flush();
out.close();
System.out.println("successfully serialized...");
} catch (Exception ex) {
System.out.println("Got exception while doing serialization... : " + ex);
}
}
}
----------------------------------------------------
----------2-----------------------------------------
----------------------------------------------------
package com.learn.java;
import java.io.Serializable;
public class StudentOfCase2 implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//adding a new field for testing...
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
------------------------------------------------
package com.learn.java;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
//case-2: serializing a custom class that implement serializable interface, but no serialversionUID.
//OUTPUT .. while trying to serialize ( without serialVersionUID) :: successfully serialized..
// Deserialize it . ( if JVM same then it will be success..
// this can fail when client-server model with diff JVM implementation)
// OUTPUT .. Name obtained after deserialziation: paramu
//successfully Deserialized...
// add a new field and try to deserialize the older serialized object. this will fail.
//OUTPUT: Got exception while doing DE-serialization... :
// java.io.InvalidClassException: com.learn.java.StudentOfCase2; local class incompatible:
// stream classdesc serialVersionUID = 2936280157816834217,
// local class serialVersionUID = 5012959175663846953
public class SerializeCase2 {
public static void main(String[] args) {
StudentOfCase2 student = new StudentOfCase2();
student.setName("paramu");
try {
FileOutputStream fos = new FileOutputStream("file1.text");
ObjectOutputStream out = new ObjectOutputStream(fos);
//SERIALIZE:
out.writeObject(student);
out.flush();
out.close();
System.out.println("successfully serialized...");
} catch (Exception ex) {
System.out.println("Got exception while doing serialization... : " + ex);
}
}
}
-----------------------------------
package com.learn.java;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class DeserializeCase2 {
public static void main(String[] args) {
try {
//DESERIALIZE:
ObjectInputStream in = new ObjectInputStream(new FileInputStream("file1.text"));
StudentOfCase2 student2 = (StudentOfCase2) in.readObject();
System.out.println(" Name obtained after deserialziation: " +student2.getName());
in.close();
System.out.println("successfully Deserialized...");
} catch (Exception ex) {
System.out.println("Got exception while doing DE-serialization... : " + ex);
}
}
}
----------------------
----------------------------------------------------
----------3-----------------------------------------
----------------------------------------------------
package com.learn.java;
import java.io.Serializable;
public class StudentOfCase3 implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//adding a new field for testing...
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
-------------------
package com.learn.java;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
//case-3: serializing a custom class that implement serializable interface, and having serialversionUID. Deserialize it .
// add a new field ( dont change the serialVersionUID) and try to deserialize the older serialized object. this will succeed.
//OUTPUT
//successfully serialized...
//OUTPUT
// Name obtained after deserialziation: paramu
//successfully Deserialized...
//OUTPUT
// Name obtained after deserialziation: paramu
//successfully Deserialized...
//OUTPUT
// Name obtained after deserialziation: paramu
// Name obtained after deserialziation: null
//successfully Deserialized...
public class SerializeCase3 {
public static void main(String[] args) {
StudentOfCase3 student = new StudentOfCase3();
student.setName("paramu");
try {
FileOutputStream fos = new FileOutputStream("file1.text");
ObjectOutputStream out = new ObjectOutputStream(fos);
//SERIALIZE:
out.writeObject(student);
out.flush();
out.close();
System.out.println("successfully serialized...");
} catch (Exception ex) {
System.out.println("Got exception while doing serialization... : " + ex);
}
}
}
---------------
package com.learn.java;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class DeserializeCase3 {
public static void main(String[] args) {
try {
//DESERIALIZE:
ObjectInputStream in = new ObjectInputStream(new FileInputStream("file1.text"));
StudentOfCase3 student3 = (StudentOfCase3) in.readObject();
System.out.println(" Name obtained after deserialziation: " +student3.getName());
System.out.println(" Name obtained after deserialziation: " +student3.getId());
in.close();
System.out.println("successfully Deserialized...");
} catch (Exception ex) {
System.out.println("Got exception while doing DE-serialization... : " + ex);
}
}
}
-----------
----------------------------------------------------
----------4-----------------------------------------
----------------------------------------------------
package com.learn.java;
import java.io.Serializable;
public class StudentOfCase4 implements Serializable {
//changing the serialVersionUID from 2L to 3L for testing deserialization.
// private static final long serialVersionUID = 2L;
private static final long serialVersionUID = 3L; //adding a new field for testing...
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
-----
package com.learn.java;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
//case-4: serializing a custom class that implement serializable interface, and having serialversionUID. Deserialize it .
//OUTPUT
//successfully serialized...
// add a new field ( and change the serialVersionUID) and try to deserialize the older serialized object. this will FAIL.
//OUTPUT
//Got exception while doing DE-serialization... : java.io.InvalidClassException: com.learn.java.StudentOfCase4;
// local class incompatible: stream classdesc serialVersionUID = 2, local class serialVersionUID = 3
// Repeat without changing the serialVersionUID) and try to deserialize the older serialized object. this will also FAIL.
//OUTPUT
//Got exception while doing DE-serialization... : java.io.InvalidClassException: com.learn.java.StudentOfCase4;
// local class incompatible: stream classdesc serialVersionUID = 2, local class serialVersionUID = 3
public class SerializeCase4 {
public static void main(String[] args) {
StudentOfCase4 student = new StudentOfCase4();
student.setName("paramu");
try {
FileOutputStream fos = new FileOutputStream("file1.text");
ObjectOutputStream out = new ObjectOutputStream(fos);
//SERIALIZE:
out.writeObject(student);
out.flush();
out.close();
System.out.println("successfully serialized...");
} catch (Exception ex) {
System.out.println("Got exception while doing serialization... : " + ex);
}
}
}
------
package com.learn.java;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class DeserializeCase4 {
public static void main(String[] args) {
try {
//DESERIALIZE:
ObjectInputStream in = new ObjectInputStream(new FileInputStream("file1.text"));
StudentOfCase4 student4 = (StudentOfCase4) in.readObject();
System.out.println(" Name obtained after deserialziation: " +student4.getName());
// System.out.println(" Name obtained after deserialziation: " +student4.getId());
in.close();
System.out.println("successfully Deserialized...");
} catch (Exception ex) {
System.out.println("Got exception while doing DE-serialization... : " + ex);
}
}
}
---------------
----------------------------------------------------
----------5-----------------------------------------
----------------------------------------------------
package com.learn.java;
import java.io.Serializable;
public class Student implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
--------------
package com.learn.java;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
//case5: ArrayList of custom class that does not implement serializable.
//Try to serialize it
//OUTPUT
// Got exception while doing serialization... : java.io.NotSerializableException: com.learn.java.Student
//Now Have the custom class implement Serializable and try serializing teh arraylist..
//OUTPUT
//successfully serialized..
//Now De-serialize it
//OUTPUT
// Name obtained after deserialziation: paramu
// successfully Deserialized...
public class SerializeCase5 {
public static void main(String[] args) {
Student student = new Student();
student.setName("paramu");
try {
FileOutputStream fos = new FileOutputStream("studentData");
ObjectOutputStream out = new ObjectOutputStream(fos);
List<Student> studentList = new ArrayList<>();
studentList.add(student);
//SERIALIZE:
out.writeObject(studentList);
out.flush();
out.close();
System.out.println("successfully serialized...");
} catch (Exception ex) {
System.out.println("Got exception while doing serialization... : " + ex);
}
}
}
-------
package com.learn.java;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;
public class DeserializeCase5 {
public static void main(String[] args) {
ArrayList<Student> studentList = new ArrayList<>(10);
try {
//DESERIALIZE:
FileInputStream fis = new FileInputStream("studentData");
ObjectInputStream in = new ObjectInputStream(fis);
studentList = (ArrayList) in.readObject();
System.out.println(" Name obtained after deserialziation: " +studentList.get(0).getName());
// System.out.println(" Name obtained after deserialziation: " +student4.getId());
in.close();
System.out.println("successfully Deserialized...");
} catch (Exception ex) {
System.out.println("Got exception while doing DE-serialization... : " + ex);
}
}
}
-------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment