Where is serialization used
This article is contributed by Mehak Narang and Shubham Juneja. If you like GeeksforGeeks and would like to contribute, you can also write an article using write. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Skip to content. Change Language.
Related Articles. Table of Contents. Save Article. Improve Article. Like Article. String name;. Previous Lambda Expressions in Java 8. Next Garbage Collection in Java. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. How setting an Object to null help Garbage Collection?
How do objects become eligible for garbage collection? How to calculate date difference in Java Difference between Path and Classpath Is Java "pass-by-reference" or "pass-by-value"? Difference between static and nonstatic methods java Why Java does not support pointers?
What is package in Java? What are wrapper classes? What is singleton class in Java? Can a top level class be private or protected in java Are Polymorphism , Overloading and Overriding similar concepts? Why can't a Java class be declared as static? Why does Java not support operator overloading?
How to generate random integers within a specific range in Java What's the meaning of System. What is the purpose of Runtime and System class? What is finally block in Java? What is difference between final, finally and finalize? What is try-with-resources in java? What is a stacktrace? Serializable interface by default. In the above example, Student class implements Serializable interface. Now its objects can be converted into stream.
The main class implementation of is showed in the next code. Only objects that support the java. Serializable interface can be written to streams. In this example, we are going to serialize the object of Student class from above code. The writeObject method of ObjectOutputStream class provides the functionality to serialize the object.
We are saving the state of the object in the file named f. Deserialization is the process of reconstructing the object from the serialized state. It is the reverse operation of serialization.
Let's see an example where we are reading the data from a deserialized object. If a class implements Serializable interface then all its sub classes will also be serializable. Let's see the example given below:. Parent class properties are inherited to subclasses so if parent class is Serializable, subclass would also be.
If a class has a reference to another class, all the references must be Serializable otherwise serialization process will not be performed. In such case, NotSerializableException is thrown at runtime.
If there is any static data member in a class, it will not be serialized because static is the part of class not object. Rule: In case of array or collection, all the objects of array or collection must be serializable. If any object is not serialiizable, serialization will be failed. The Externalizable interface provides the facility of writing the state of an object into a byte stream in compress format. It is not a marker interface. Now, id will not be serialized, so when you deserialize the object after serialization, you will not get the value of id.
It will return default value always. In such case, it will return 0 because the data type of id is an integer. The serialization process at runtime associates an id with each Serializable class which is known as SerialVersionUID.
0コメント