English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Serialization is the process of persisting Java objects in the form of a byte sequence, which includes the data of the object as well as information about the type of the object and the data types stored in the object. Serialization is the process of/The state is converted to bytes for transmission over the network or for storage. On the other hand, deserialization is the process of converting byte code back into the corresponding Java object.
Transient variables are variables whose values are not serialized during the serialization process. When we deserialize this variable, we will obtain the default value of the variable.
private transient <member-variable>;
import java.io.*; class EmpInfo implements Serializable { String name; private transient int age; String occupation; public EmpInfo(String name, int age, String occupation) { this.name = name; this.age = age; this.occupation = occupation; } public String toString() { StringBuffer sb = new StringBuffer(); sb.app*end("Name:");+"\n"); sb.append(this.name+"\n"); sb.append("Age:");+ "\n"); sb.append(this.age + "\n"); sb.append("Occupation:"); + "\n"); sb.append(this.occupation); return sb.toString(); } } // main class public class TransientVarTest { public static void main(String args[]) throws Exception { EmpInfo empInfo = new EmpInfo("Adithya", 30, "Java Developer"); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("empInfo")); oos.writeObject(empInfo); oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("empInfo")); EmpInfo empInfo1 = (EmpInfo)ois.readObject(); System.out.println(empInfo1); } }
Output Result
Name: Adithya Age: 0 Occupation: Java Developer