Explain the Java I/O streaming concept and the use of the decorator design pattern in Java I/O?

Java input and output is defined in terms of an abstract concept called a “stream”, which is a sequence of data. There are 2 kinds of streams. Byte streams (8 bit bytes) -> Abstract classes are: InputStream and OutputStream Character streams (16 bit UNICODE) -> Abstract classes are: Reader and Writer Design pattern: java.io.* classes use the decorator design pattern. The decorator design pattern attaches responsibilities to objects at runtime. Decorators are more flexible than inheritance because the inheritance attaches responsibility to classes at compile time. The java.io.* classes use the decorator pattern to construct different combinations of behavior at … Click here to continue…..

What is Java Serial Version ID?

Say you create a “Car” class, instantiate it, and write it out to an object stream. The flattened car object sits in the file system for some time. Meanwhile, if the “Car” class is modified by adding a new field. Later on, when you try to read (i.e. deserialize) the flattened “Car” object, you get the java.io.InvalidClassException – because all serializable classes are automatically given a unique identifier. This exception is thrown when the identifier of the class is not equal to the identifier of the flattened object. If you really think about it, the exception is thrown because of … Click here to continue…..

When to use serialization?

Do not use serialization if you do not have to. A common use of serialization is to use it to send an object over the network or if the state of an object needs to be persisted to a flat file or a database. Deep cloning or copy can be achieved through serialization. This may be fast to code but will have performance implications. To serialize the above “Car” object to a file (sample for illustration purpose only, should use try {} catch {} block):

The objects stored in an HTTP session should be serializable to support in-memory replication … Click here to continue…..

How would you exclude a field of a class from serialization or what is a transient variable?

Transient variables cannot be serialized. The fields marked transient in a serializable object will not be transmitted in the byte stream. An example would be a file handle, a database connection, a system thread etc. Such objects are only meaningful locally. So they should be marked as transient in a serializable class. Serialization can adversely affect performance since it: Depends on reflection. Has an incredibly verbose data format. Is very easy to send surplus data.

What is serialization?

Serialization is a process of reading or writing an object. It is a process of saving an object’s state to a sequence of bytes, as well as a process of rebuilding those bytes back into a live object at some future time. An object is marked serializable by implementing the java.io.Serializable interface, which is only a marker interface — it simply allows the serialization mechanism to verify that the class can be persisted, typically to a file.