Tuesday, June 10, 2008

Question: What is serialization ?

Answer: Serialization is the process of writing complete state of java object into output stream, that stream can be file or byte array or stream associated with TCP/IP socket.

Question: What does the Serializable interface do ?

Answer: Serializable is a tagging interface; it prescribes no methods. It serves to assign the Serializable data type to the tagged class and to identify the class as one which the developer has designed for persistence. ObjectOutputStream serializes only those objects which implement this interface.

Question: How do I serialize an object to a file ?

Answer: To serialize an object into a stream perform the following actions:

- Open one of the output streams, for exaample FileOutputStream
- Chain it with the ObjectOutputStream - Call the method writeObject() providingg the instance of a Serializable object as an argument.
- Close the streams

Java Code --------- try{ fOut= new FileOutputStream("c:\\emp.ser"); out = new ObjectOutputStream(fOut); out.writeObject(employee); //serializing System.out.println("An employee is serialized into c:\\emp.ser"); } catch(IOException e){ e.printStackTrace(); }

Question: How do I deserilaize an Object?

Answer: To deserialize an object, perform the following steps:

- Open an input stream
- Chain it with the ObjectInputStream - Call the method readObject() and cast tthe returned object to the class that is being deserialized.
- Close the streams

Java Code try{ fIn= new FileInputStream("c:\\emp.ser"); in = new ObjectInputStream(fIn); //de-serializing employee Employee emp = (Employee) in.readObject(); System.out.println("Deserialized " + emp.fName + " " + emp.lName + " from emp.ser "); }catch(IOException e){ e.printStackTrace(); }catch(ClassNotFoundException e){ e.printStackTrace(); }

Question: What is Externalizable Interface ?

Answer : Externalizable interface is a subclass of Serializable. Java provides Externalizable interface that gives you more control over what is being serialized and it can produce smaller object footprint. ( You can serialize whatever field values you want to serialize)

This interface defines 2 methods: readExternal() and writeExternal() and you have to implement these methods in the class that will be serialized. In these methods you'll have to write code that reads/writes only the values of the attributes you are interested in. Programs that perform serialization and deserialization have to write and read these attributes in the same sequence.

Question: Explain garbage collection ?

Answer: Garbage collection is an important part of Java's security strategy. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects from the memory. The name "garbage collection" implies that objects that are no longer needed by the program are "garbage" and can be thrown away. A more accurate and up-to-date metaphor might be "memory recycling." When an object is no longer referenced by the program, the heap space it occupies must be recycled so that the space is available for subsequent new objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed

Question : How you can force the garbage collection ?

Answer : Garbage collection automatic process and can't be forced. We can call garbage collector in Java by calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected.

Question : What are the field/method access levels (specifiers) and class access levels ?

Answer: Each field and method has an access level:

  • private: accessible only in this class
  • (package): accessible only in this package
  • protected: accessible only in this package and in all subclasses of this class
  • public: accessible everywhere this class is available

Similarly, each class has one of two possible access levels:

  • (package): class objects can only be declared and manipulated by code in this package
public: class objects can be declared and manipulated by code in any package

1 comment:

Venugopal Reddy said...
This comment has been removed by the author.