0% found this document useful (0 votes)
81 views

1) Difference Between Transient Vs Volatile Variable or Modifier in Java?

Transient variables are not serialized, while volatile variables ensure visibility across threads. Serializable uses default serialization, while Externalizable requires implementing serialization methods. Serialization persists an object's state to storage or for transfer between JVMs, by implementing Serializable classes are serialized, writing the object to an ObjectOutputStream.

Uploaded by

riyaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

1) Difference Between Transient Vs Volatile Variable or Modifier in Java?

Transient variables are not serialized, while volatile variables ensure visibility across threads. Serializable uses default serialization, while Externalizable requires implementing serialization methods. Serialization persists an object's state to storage or for transfer between JVMs, by implementing Serializable classes are serialized, writing the object to an ObjectOutputStream.

Uploaded by

riyaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1)

Difference between transient vs volatile variable or modifier


in Java?

By making a variable or field transient in a Class prevents it from being


Serialized in Java. Along with static variables, transient variables are not
serialized during Serialization and they are initialized with there default value
during deserialization process e.g. an int transient variable is initialized with
zero during deserialization in Java.
On the other hand volatile variables are used in
Concurrent PROGRAMMING in Java. When we declare a variable volatile,
every thread reads its value from main memory and don't used cached value
available in every thread stack. volatile variable also prevents compiler from
doing reordering which can compromise synchronization

2) Difference between Serializable vs Externalizable in Java?


1) One of the obvious difference between Serializable and Externalizable is
that Serializable is a marker interface i.e. does not contain any method but
Externalizable
interface
contains
two
methods writeExternal() and readExternal().
2) Second difference between Serializable vs Externalizable is responsibility of
Serialization. when a class implements Serializable interface, default
Serialization process gets kicked of and that takes responsibility of serializing
super class state. When any class in Java implementjava.io.Externalizable than
its your responsibility to implement Serialization process i.e. preserving all
important
information.
3) This difference between Serializable and Externalizable is performance.
You can not do much to improve performance of default serialization process
except reducing number of fields to be serialized by using transient and static
keyword but with Externalizable interface you have full control over
Serialization process.

3) What is serialization in java?


Serialization in Java is a process to persist any Java Object's state into File
System or convert them into byte stream to transfer over network to other JVM
or program. Serialization in Java is done by JVM by employing default
Serialization process which persist all of Object's state except transient
variable and static variable.
For a java class to be serialize class needs to
implements java.io.Serializable interface and JVM will automatically serialize its
instance when passed over to java.io.ObjectOutputStream using writeObject().
Serializable interface is also called marker interface or tag interface because it

does not contain any method or behavior and just used to tag a class, by
seeing a class implementing Serializable JVM knows that it needs to serialize
instance of this class.
4) serialization example

importjava.io.*;
publicclassSerializeDemo{
publicstaticvoidmain(String[]args){
Employeee=newEmployee();
e.name="ReyanAli";
e.address="PhokkaKuan,AmbehtaPeer";
e.SSN=11122333;
e.number=101;
try{
FileOutputStreamfileOut=newFileOutputStream("/tmp/employee.ser");
ObjectOutputStreamout=newObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serializeddataissavedin/tmp/employee.ser");}
catch(IOExceptioni)
{
i.printStackTrace();
}
}
}

DeSerialize
import java.io.*;
public class DeserializeDemo
{
public static void main(String [] args)
{
Employee e = null;
try
{
FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");

}
}

System.out.println("Name: " + e.name);


System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);

You might also like