Serialization
Serialization
SERIALIZATION
● Objects in Java are stored in memory. Whenever the objects are not used, it is cleaned by
Java’s garbage collector. However, if an object needs to be sent over a network, it is
necessary to convert the object into a byte stream. This process is facilitated by
serialization.
● Serialization can be defined as the process of converting the state of an object to a byte
stream.
● The primary advantage of this mechanism lies in the fact that it is platform independent, that
is, objects that are serialized in one platform can be transferred over a network and used in
any other platform on deserialization of the object.
● Serializable is a marker interface, which means it has no fields or methods. It only adds
special behavior to the file.
● If the fields of the class should not be made serializable, they should be declared as
transient.
● All the non-static variables that are not transient are serializable.
● String class and all wrapper classes implement Serializable interface by default.
DESERIALIZATION
● Distributed systems store objects across different nodes. In order to fetch an object from
one node to another, a system must be able to deserialize it from raw bytes.
● Deserialization is the process of converting a byte stream into the original object.
● It uses the information about the class that is stored in the byte stream for creating a new
instance of the object and assigning the instance variables the values stored in the stream.
HASHMAP
● Hashmaps are not synchronized, which means that if multiple threads access the same
hashmap concurrently, it can lead to inconsistent data.
● It enables faster access to the values based on the uniquely identified keys.
● Each key must be unique. However, if a key that already exists is added again, the old
value will be replaced by the new value.
● It allows storing null elements. However, only one null key can exist.
CODE
import java.io.*; import java.util.*; class Config implements Serializable
{
int serialVersionUID = 1;
HashMap<String, String> values; public Config()
{
this.values = new HashMap<String, String>();
}
public String get(String key)
{
return values.get(key);
}
public void set(String key, String value)
{
values.put(key, value);
}
public void print()
{
System.out.println("Current Config Values: "); for
(String key : values.keySet())
{
System.out.println(key + " = " + values.get(key));
}
}
public static Config load() {
Config config = null; try
{
FileInputStream inp = new
FileInputStream("config.cfg");
ObjectInputStream out = new ObjectInputStream(inp); config =
(Config) out.readObject(); out.close();
//out.close();
System.out.println("Loaded Config from config.cfg");
} catch (IOException | ClassNotFoundException e) {
System.out.println("Unable to load config.cfg, creating new
Config"); config = new Config();
}
return config;
}
public void save()
{ try
{
FileOutputStream fileOut = new FileOutputStream("config.cfg");
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
objOut.writeObject(this); objOut.close(); fileOut.close();
System.out.println("Saved Config to config.cfg");
}
catch (IOException err)
{
err.printStackTrace();
}
}
public static void main(String[] args)
{
Config config = Config.load(); config.print(); config.set("Path",
"/ooad_assignment/lab6"); config.set("Version", "1.0");
config.set("System_Name", "PES2UG20CS164");
config.print(); config.save();
}
}
OUTPUT: