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)

When a Serializable object is written with writeObject, then modified and written a second time, why is the modification missing when the stream is deserialized

Java   JSP   ORACLE   Hibernate   Servlet   Struts   Spring   MySQL   Java Script     Post New Topic
Category Java
Asked By : sa Asked Date : May 19 2008
Brief Desc: When a Serializable object is written with writeObject, then modified and written a second time, why is the modification missing when the stream is deserialized
Details: The ObjectOutputStream class keeps track of each object it serializes and sends only the handle if the object is written into the stream a subsequent time. This is the way it deals with graphs of objects. The corresponding ObjectInputStream keeps track of all of the objects it has created and their handles so when the handle is seen again it can return the same object. Both output and input streams keep this state until they are freed.

Alternatively, the ObjectOutputStream class implements a reset method that discards the memory of having sent an objecct, so sending an object again will make a copy

Replied By: satya Replied Date : May 19 2008
Answer: Why is OutOfMemoryError thrown after writing a large number of objects into an ObjectOutputStream ?

answer :
The ObjectOutputStream maintains a table mapping objects written into the stream to a handle. The first time an object is written to a stream, its contents are written into the stream; subsequent writes of the object result in a handle to the object being written into the stream. This table maintains references to objects that might otherwise be unreachable by an application, thus, resulting in an unexpected situation of running out of memory. A call to the ObjectOutputStream.reset() method resets the object/handle table to its initial state, allowing all previously written objects to be elgible for garbage collection.

Replied By: ss Replied Date : May 19 2008
Answer: How do I get the serialVersionUID of an array class?
answer :

Run the serialver tool, supplying the name of the class, as shown in the example that follows:

serialver "[Ljava.lang.String;"

Replied By: satya Replied Date : May 19 2008
Answer: How do I serialize a tree of objects?

import java.io.*;

class tree implements java.io.Serializable {
public tree left;
public tree right;
public int id;
public int level;
private static int count = 0;
public tree(int l) {
id = count++;
level = l;
if (l > 0) {
left = new tree(l-1);
right = new tree(l-1);
public void print(int levels) {
for (int i = 0; i < level; i++)
System.out.print(" ");
System.out.println("node " + id);

if (level <= levels && left != null)

if (level <= levels && right != null)

public static void main (String argv[]) {

try {
/* Create a file to write the serialized tree to. */
FileOutputStream ostream = new FileOutputStream("tree.tmp");
/* Create the output stream */
ObjectOutputStream p = new ObjectOutputStream(ostream);

/* Create a tree with three levels. */

p.writeObject(base); // Write the tree to the stream.
ostream.close(); // close the file.

/* Open the file and set to read objects from it. */
FileInputStream istream = new FileInputStream("tree.tmp");
ObjectInputStream q = new ObjectInputStream(istream);

/* Read a tree object, and all the subtrees */
tree new_tree = (tree)q.readObject();

new_tree.print(3); // Print out the top 3 levels of the tree
} catch (Exception ex) {

Replied By: satya Replied Date : May 19 2008
Answer: If class A does not implement Serializable but a subclass B implements Serializable, will the fields of class A be serialized when B is serialized?

Only the fields of Serializable objects are written out and restored. The object may be restored only if it has a no-arg constructor that will initialize the fields of non-serializable supertypes. If the subclass has access to the state of the superclass it can implement writeObject and readObject to save and restore that state.

Replied By: nick Replied Date : May 19 2008
Answer: Does object serialization support encryption?
Object serialization does not contain any encryption/decryption in itself. It writes to and reads from Java Streams, so it can be coupled with any available encryption technology. Object serialization can be used in many different ways from simple persistence, writing and read to/from files, or for RMI to communicate across hosts.

RMI's use of serialization leaves encryption and decryption to the lower network transport. We expect that when a secure channel is needed, the network connections will be made using SSL or the like

Replied By: raj Replied Date : May 19 2008
Answer: This helps lot

Add Your Reply :

Your Name:

  • SCJP 5.0 Simulator 642+ Questions With Explanations
  • SCJP 6.0 Simulator 664+ Questions With Explanations
  • SCWCD 5.0 Simulator 556+ Questions With Explanations
  • SCWCD 4.0 Simulator 500+ Questions With Explanations
  • OCA 10g Simulator 594+ Questions With Explanations
  • Struts Tutorial Home
  • Spring Tutorial Home
  • Hibernate Tutorial Home
  • JSP Tutorial Home
  • JDBC Tutorial Home
  • Webservices Tutorial Home
  • EJB Fundamentals
  • Java mock test.
  • JSP mock test.
  • ORACLE mock test.
  • Hibernate mock test.
  • Servlet mock test
  • Struts mock test
  • C mock test
  • C++ mock test
  • Aptitude mock test
  • Interview Questions from Experts

    Reviews and Comments

    PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

    Copyright © www.techfaq360.com 2019

    About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy