Presents your JAVA E-NEWSLETTER for November 6, 2003 <-------------------------------------------> COMPARING JAVA.IO.EXTERNALIZABLE VS. JAVA.IO.SERIALIZABLE Even if you haven't used object serialization, you've probably heard about it. But do you know about another form of object permanence that Java supports called externalization? Here's how serialization and externalization relate on a code level: public interface Serializable {} public interface Externalizable extends Serializable { void readExternal(ObjectInput in); void writeExternal(ObjectOutput out); } KEY DIFFERENCES BETWEEN SERIALIZATION AND EXTERNALIZATION Externalization and serialization are two different ways of achieving the same goal. Let's examine the two key differences between serialization and externalization. Support for object serialization via the Serializable interface is built into the core API, but all implementers of java.io.Externalizable must provide the implementation for reading and writing objects. Java has built-in support for serialization, which means that as long as you make your classes java.io.Serializable, Java will attempt to store and reconstitute your objects. With externalization, you've elected to do all the work of reading and writing objects yourself; the only support Java supplies is the interface: void readExternal(ObjectInput in) void writeExternal(ObjectOutput out) Now, what you do in the readExternal() and writeExternal() methods is up to you. Serialization automatically stores information needed to deserialize the stored instance, while externalization saves only the identity of the class being stored. When you serialize an object via the java.io.Serializable interface, information about the class, such as its attributes and their types, will be stored along with the instance data. When you go the Externalizable route, Java saves very little information about each type stored. PROS AND CONS OF EACH INTERFACE SERIALIZABLE * PRO: Built-in support * PRO: Easy-to-implement * CON: More overhead in size * CON: May be slower because of additional overhead EXTERNALIZABLE * PRO: Less overhead (what's stored is up to the programmer) * PRO: Possible speed improvements * CON: The virtual machine doesn't provide any help, which means that all of the work falls on the developers' shoulders. Whichever solution you choose should depend on your application's requirements. Serializable is usually the easiest solution, but it may introduce performance or space issues that are unacceptable; in that case, Externalizable may be the way to go. Keep in mind that if a class is Externalizable, then the Externalizable methods will be used to serialize instances of the class even if the type provides the Serializable methods: private void writeObject() private void readObject() David Petersheim is a Senior Java Developer with Genscape, Inc. He designs and develops server-side applications to acquire and process real-time energy data. ----------------------------------------