1) Difference Between Transient Vs Volatile Variable or Modifier in Java?
1) Difference Between Transient Vs Volatile Variable or Modifier in Java?
does not contain any method or behavior and just used to tag a class, by
seeing a class implementing Serializable JVM knows that it needs to serialize
instance of this class.
4) serialization example
importjava.io.*;
publicclassSerializeDemo{
publicstaticvoidmain(String[]args){
Employeee=newEmployee();
e.name="ReyanAli";
e.address="PhokkaKuan,AmbehtaPeer";
e.SSN=11122333;
e.number=101;
try{
FileOutputStreamfileOut=newFileOutputStream("/tmp/employee.ser");
ObjectOutputStreamout=newObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serializeddataissavedin/tmp/employee.ser");}
catch(IOExceptioni)
{
i.printStackTrace();
}
}
}
DeSerialize
import java.io.*;
public class DeserializeDemo
{
public static void main(String [] args)
{
Employee e = null;
try
{
FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
}
}