How does serialization work

Answers were Sorted based on User's Feedback



How does serialization work..

Answer / sudhakar rao

Serialization is a marker interface to tell the jvm that
the class extending Serializable interface wants to persist
or write its state over the output stream. All the
variables other than transient type can only be persisted.

Is This Answer Correct ?    9 Yes 0 No

How does serialization work..

Answer / bindhu

The class whose instances are to be serialized
should implement an interface Serializable. Then pass the
instances to the ObjectOutputStream which is connected to a
FileOutputStream. This will save the object to a file...
When an object ios serialized, all the included
objects are also serialized along with the original object.

Is This Answer Correct ?    4 Yes 2 No

How does serialization work..

Answer / amit singh

serializtion just to serialize a obect of a particular
class mean to say that
its just to save the state of object of particular clas
how imagine

SERIALIZATION:
a class Xyz
this class implements the Serializable interface
then you provide some state to create its object
and want that its state will remain safe
so what you should to do
you are using a
"FileOutputstream" to create a
than a "ObjectOutputStream" And then use writeObject() mehod
to write it in file

FileOutputStream f1 = new FileOutputStream("f1.txt");
ObjectOutputStream o1 = new ObjectOutStream(f1);
o1.writeObject(refreance variable of particular class)


then what happen the state of the particular object or
means some value which you gave its instance variable
or whole object will convert in a byte and store in file
.its a serializtion

DESERIALIZATION:
so the constructor first time will execute during the
creation time.
but when you desrialize constructor will not execute
because of the serializable interface so
when you want to read the state or any value which you gave
at the time of the serializtion will comeup as it is

FileInputStream f2 = new FileInputStream("f1.txt");
ObjectInputStream f3 = new ObjectInputStream(f2);
f3.readObject();//return type object type so cast it

and youwill get the same state of object which you will
make at the time of searlization.

"so the searliztion is just to saving the object in a form
of byte in a storage medium or write it in file using the
i/o
and then deserialize is convert the byte in to a particular
object with the same state when the object created"
i want to see you some code :

class Amit implements Serializable
{
String name;
int age;
Amit(Strin name,int age)
{
this.name = name;
this.age = age;
}
public Strin toString()
{
return "name =" + name +";+ age =" + age;
}
}

class Xyz
{
publc static void main(string []args)
{
Amit a = new Amit("amit",26);
//serialization
try{
FileOutputStram f1 = new FileOutputream("file.txt");
ObjectOutputStream o1 = new ObjectOutputStream(f1);
o1.writeObject(a);
o1.close();
}
catch(Excetion e)
{
}
//desrialization
try{
FileInputStream f2 = new FileInputStream("file.txt");
ObjectInputStream o2 = new ObjectInputStream(f2);
Amit a1 = o2.writeObject(a);
System.out.println("value" + a1);
System.out.println("value" + a1.toString());
}
catch(Exception e)
{
}
}
thanks Amit singh
amitsin2008@gmail.com amit09mca
}
//sorry :-import the io package too

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More Core Java Interview Questions

Differentiate between == and equals().

0 Answers  


difference between String a; and String a=new String();? y do v need to assign memory to the variable?

2 Answers  


How does the garbage collector works in java?

0 Answers   Cyient,


If I will write String s=new String("XYZ"); String s1=new String("XYZ"); if(s.equals(s1)){ sop("True"); } else{ sop("False"); } This program will give me "True". But When I am creating my own class suppose class Employee{ public Employee(String name); } Employee e= new Employee("XYZ"); Employee e1 = neew Employee("XYZ"); if(e.equals(e1)){ sop("True"); } else{ sop("False"); } Then it will give the output as "False". Can I know what is happening internally?

5 Answers  


When a lot of changes are required in data, which one should be a preference to be used? String or stringbuffer?

0 Answers  


How do you do exponents in java?

0 Answers  


Difference between abtsract & final

1 Answers   Nous,


which one is performance wise advantageious from List,Set,Map?

6 Answers  


What is string immutability?

0 Answers  


how to write a server program and sending the mails to the server using smtp protocol please help me

0 Answers   Lampex,


Is java ee a framework?

0 Answers  


What does it mean that a method or field is “static”?

0 Answers  


Categories