Introduction
Suppose I have some raw fish and want to keep them fresh for 1 or 2 days and eat them later. Now what I will have to do to keep them fresh I will just freeze them by keeping them in the refrigerator. But at the time of consumption, I will have to defreeze them. In simple words, freezing the item into an icy form is called serialization. The reverse process of converting an icy form into a normal form is called de-serialization.
Now in computing, it’s quite similar to the above example but in a quite technical way. Here in this article, we will see how serialization and de-serialization work in java.
What is Serialization and Deserialization in Java?
Serialization is a process of converting an object into a byte sequence, from java supported form into file supported form or network supported form. And the reverse process where the sequence of a byte is used to create further objects to convert the object from file supported form into java supported form is called de-serialization.
Don’t get messed up, and it’s not the process of saving the state of an object to the file; strictly speaking, it’s the process of converting an object from java supported form to file supported form or network supported form.
How serialization works?
Consider a JVM with an object that has a particular value and that JVM is running in a machine, say MACHINE 1. Now let us consider that we need to create this JVM again in another machine, say MACHINE 2, with the same object and same object value. So this is the condition where serialization technique will be required. JVM do this serialization process by creating a sequence of byte that only machine and network understands and ships those sequences of bytes over the network. The process starts only when we implement the interface called serializable, Java.io.Serializable.
Machine 1
Machine 2
Serialization process
import java.io.*; It’s the package that is required to import at the beginning of a serialization program, without which the compiler will show an error as an output message.
The FileOutputStream is a class used to connect to a preexisting file or create a file and then connect to it.
FileOutputStream fos=new FileOutputStream(“abc.ser”);
The file ObjectOutputStream is the class that is used to write object inside the file with the help of FileoutputStream.
ObjectOutputStream os = new ObjectOutputStream (fos);
Deserialization process
The FileInputStream is a class that connects a file for deserialization
FileInputStream fin = new FileInputStream(“abc.ser”);
And if the file is not present the it will send the message
File not exist
The ObjectInputStream is the class that is used mainly to convert the data of the file into an object.
ObjectInputStream is = new ObjectInputStream (fis);
SERIALIZATION EXAMPLE
import java.io.*;
class Doll implements Serializable
{
int i = 10;
int j = 20;
}
class SerializeDemo
{
public static void main(String[] args) throws Exception
{
Doll d1= new Doll();
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos. writeObject(d1);
System.out.println("success");
}
}
OUTPUT
java SerializeDemo
Process started (PID=4536) >>>
success
In the above example a new file is first created then the values of the object are serialized. The values of the object are converted into a stream of bytes.
DESERIALIZATION EXAMPLE
import java.io.*;
class Doll implements Serializable
{
int i = 10;
int j = 20;
}
class SerializeDemo
{
public static void main(String[] args) throws Exception
{
Doll d1= new Doll();
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos. writeObject(d1);
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Doll d2 = (Doll)ois.readObject();
System.out.println(d2.i+"__"+d2.j);
}
}
OUTPUT
java SerializeDemo
Process started (PID=12356) >>>
10__20
Here in the above example, the byte stream of the files is deserialized and converted into java supported form.
The serialized version of the contents of the object abc.ser
¬í sr DollxRl¸ßš€ I ixp
What is Transient Keyword
Transient is the modifier that is only applicable to variables. Suppose we have multiple variables, but some values or data in the variable are sensitive or say confidential, and we don’t want to share this data with anyone, then we must consider those variables as transient, i.e. if we don’t want to save the value of any variable to meet the security constraint at the time of serialization then it is called transient. The value that is considered transient during serialization the value will not be saved, and after the deserialization we will not get that value of the variable.
SYNTAX
For making any value transient, just put the keyword transient before the value transient int j = 20;
Example
import java.io.*;
class Doll implements Serializable
{
int i = 10;
transient int j = 20;
}
class SerializeDemo
{
public static void main(String[] args) throws Exception
{
Doll d1= new Doll();
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos. writeObject(d1);
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Doll d2 = (Doll)ois.readObject();
System.out.println(d2.i+"__"+d2.j);
}
}
Output
java SerializeDemo
Process started (PID=12356) >>>
10__0
Here the value of j becomes 0 cause j was not serialized as we put the transient keyword before j.
The transient keyword can only be used for class-level variable; remember this, not for the object variable.
What is a static variable?
Static variables are those which can not be serialized, and after the deserialization process, they will load from the class.
What are instance variable?
Instance variables are those which can be serialized, and during deserialization we will get back values in their original state.
What is the need for serialization in java?
1. Serialization is the technique that allows us to save the state of an object into a sequence of byte and helps saving it on the hard disk or over the network to other machines.
2. Serialization helps in saving time by sending the data, i.e. the object from one machine to another machine
3. Serialization is free to use for a larger set of arbitrarily complicated data.
4. The serialization is a platform-independent process. An object that is serialized on one platform can be deserialized on a various other platforms.
Scanner class in Java
Introduction
User input is one of the most important parts of any programming language. Java is one of those high-level languages that allows user to give inputs from the keyboard. Here in this article, you will get a brief overview of the process behind it.
What is scanner class in java?
Scanner class is something that is used to get input like int, double, string etc. from the user using the keyboard.
Importing the scanner class
At first, a class called java.util.Scanner needs to be imported. Then an object of the scanner class is to be created. The input is taken using inbuilt methods.
Methods in scanner class
nextBoolean() | It is used to read a boolean value from the user |
nextByte() | Used to read a byte value from the user |
nextDouble() | Used to read a double value from the user |
nextFloat() | Used to read a float value from the user |
nextInt() | Used to read an int value from the user |
nextLine() | Used to read a String value from the user |
nextLong() | Used to read a long value from the user |
nextShort() | Used to read a short value from the user |
How to use scanner class
Syntax:
Scanner myObj = new Scanner(System.in);
Here in the above syntax, the first scanner is the name of the class, myObj is the variable, the second scanner is the constructor of the scanner class and system.in is the parameter. It means that we are creating an object that reads data from the parameter. System.in is the input string that pointing towards the keyboard. It is used to read inputs from the keyboard.
Example
import java.util.Scanner;
class check1 {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter name, age and salary:");
String name = myObj.nextLine();
int age = myObj.nextInt();
double salary = myObj.nextDouble();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}
Output
java check1
Process started (PID=11700) >>>
Enter name, age and salary:
ravi
45
45000
Name: ravi
Age: 45
Salary: 45000.0
In the above example, the user is asked to give the inputs like name, age and salary, and in turns, the computer displays the output. We have used three methods next line, nextInt and nextDouble. The next line will read a string; the nextInt will read integer value, the nextDouble will read a double value from the user.
If you wish to learn more about java, try this free java programming course and enhance your skill.
Engaging in the study of Java programming suggests a keen interest in the realm of software development. For those embarking upon this journey with aspirations towards a career in this field, it is recommended to explore the following pages in order to acquire a comprehensive understanding of the development career path: