|Home |Login |Registration |Struts Step by Step Tutorial |Hibernate Step by Step Tutorial |Spring Step by Step Tutorial |JSP Step by Step Tutorial |JDBC Step by Step Tutorial |Web Services Step by Step Tutorial |EJB fundamentals | ORACLE Step by Step Tutorial | SCJP 5.0 and SCJP 6.0 Study Guide | SCWCD 5.0 Study Guide | SCJP Tips
Java mock test | SCJP mock test | SCJP DUMP | SCBCD mock test |Java online test exam | JSP online test | ORACLE online test | Hibernate online test | Servlet online test | Struts online test | EJB online test | C online test | C++ online test | Aptitude online test

Java interview questions | JSP interview questions | ORACLE interview questions | Hibernate interview questions | Servlet interview questions | Struts interview questions | JDBC interview questions | C/C++ interview questions | Spring interview questions | JMS interview questions | Informatica interview questions | EJB interview questions | OOPS and Design Pattern interview questions
1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc) JobsJobs and Walkins

Java Tutorials

Q.What is serialization? Explain with example?

Serialization involves saving the current state of an object to a stream, and restoring an equivalent object from that stream.

For an object to be serialized, it must be an instance of a class that implements either the Serializable or Externalizable interface. Both interfaces only permit the saving of data associated with an object's variables.
They depend on the class definition being available to the Java Virtual Machine at reconstruction time in order to construct the object.
The Serializable interface relies on the Java runtime default mechanism to save an object's state.
Writing an object is done via the writeObject() method in the ObjectOutputStream class (or the ObjectOutput interface).
Writing a primitive value may be done through the appropriate write<datatype>() method.
Reading the serialized object is accomplished using the readObject() method of the ObjectInputStream class, and primitives may be read using the various read<datatype>() methods.

The Externalizable interface specifies that the implementing class will handle the serialization on its own, instead of relying on the default runtime mechanism.
This includes which fields get written (and read), and in what order.
The class must define a writeExternal() method to write out the stream, and a corresponding readExternal() method to read the stream.
Inside of these methods the class calls ObjectOutputStream writeObject(), ObjectInputStream readObject(), and any necessary write<datatype>() and read<datatype>() methods, for the desired fields.

class DataOrder implements Serializable{
String apples, peaches, pears, cardnum, custID;
double icost;
int itotal;
}

Saving the current state of an object to a stream
DataOrder orders = new DataOrder();
FileOutputStream fos =
new FileOutputStream(orders);
ObjectOutputStream oos =
new ObjectOutputStream(fos);
oos.writeObject(order);

restoring an equivalent object from that stream
FileInputStream fis =
new FileInputStream(orders);
ObjectInputStream ois =
new ObjectInputStream(fis);
order = (DataOrder)ois.readObject();

Suggested Jobs

   More Jobs >>

The information you are posting should be related to java and ORACLE technology. Not political.