java bean如果不进行序列化会发生什么?

代码参考:https://www.jb51.net/article/118646.htm

什么是序列化?将对象的状态信息转换为可以存储或传输的形式的过程,在序列化期间,对象将其当前状态写入到临时存储区或持久性存储区,之后,便可以通过从存储区中读取或反序列化对象的状态信息,来重新创建该对象。

java序列化的例子:

public class Employee implements java.io.Serializable {     public String name;     public String address;     public transient int SSN;     public int number;     public void mailCheck()     {         System.out.println("Mailing a check to " + name                 + " " + address);     } }
public class SerializeDemo {     public static void main(String [] args)     {         Employee e = new Employee();         e.name = "Reyan Ali";         e.address = "Phokka Kuan, Ambehta Peer";         e.SSN = 11122333;         e.number = 101;         try         {             FileOutputStream fileOut =                     new FileOutputStream("p://employee.ser");             ObjectOutputStream out = new ObjectOutputStream(fileOut);             out.writeObject(e);             out.close();             fileOut.close();             System.out.println("Serialized data is saved in employee.ser");         }catch(IOException i)         {             i.printStackTrace();         }     } }

反序列化:

import java.io.*; public class DeserializeDemo {     public static void main(String [] args)     {         Employee e = null;         try         {             FileInputStream fileIn = new FileInputStream("p://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...");         System.out.println("Name: " + e.name);         System.out.println("Address: " + e.address);         System.out.println("SSN: " + e.SSN);         System.out.println("Number: " + e.number);     } }

以上代码正常执行,如果改为:

public class Employee {     public String name;     public String address;     public transient int SSN;     public int number;     public void mailCheck()     {         System.out.println("Mailing a check to " + name                 + " " + address);     } }

则写入文件时会报错:

java.io.NotSerializableException: com.Employee
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at com.sunlands.community.api.message.REST.SerializeDemo.main(SerializeDemo.java:24)

第24行代码报错:即:out.writeObject(e);