[go: up one dir, main page]

0% found this document useful (0 votes)
10 views4 pages

Serialization

The document discusses serialization and deserialization in Java. Serialization is the process of converting an object into a byte stream, allowing it to be saved or sent over a network. Deserialization converts the byte stream back into an object. The document provides details on serialization, deserialization, and the HashMap class in Java.

Uploaded by

komal.260186
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Serialization

The document discusses serialization and deserialization in Java. Serialization is the process of converting an object into a byte stream, allowing it to be saved or sent over a network. Deserialization converts the byte stream back into an object. The document provides details on serialization, deserialization, and the HashMap class in Java.

Uploaded by

komal.260186
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

UE20CS351

OOAD JAVA LAB


WEEK 6

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.

● Serialization can also be used to save the state of an object.

● Objects have to implement the java.io.Serializable interface in order to be serializable.

● 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.

● Each serializable class is associated with an ID called SerialVersionUID, which is used to


verify the sender and the receiver of the object.

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 is the reverse process of serialization.

● 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

● Hashmap is a class in Java that implements the Map interface.

● It allows storing of key-value pairs, in which the keys are unique.

● It is made available through the java.util package.

● 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.

● The key-value pairs are stored in an unordered fashion.

● 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:

You might also like