Sitemap

Head First Java Chapter 14 — Saving Objects

4 min readJun 26, 2022

--

Greetings all,

This article is to highlight the main points include in the chapter.

Java Serialization

Java has a feature called serialization that allows you to save an object’s state as a byte-stream. Hibernate, RMI, JPA, EJB, and JMS technologies are where it is most frequently encountered.

Deserialization, which transforms a byte-stream into an object, is the opposite of serialization. You can serialize an item on one platform and deserialize it on another because the serialization and deserialization operations are platform-independent.

We serialize the item using the ObjectOutputStream class’s writeObject() function and deserialize it using the ObjectInputStream class’ readObject() method. We must implement the Serializable interface in order to serialize the object.

Advantage: It is primarily used to traverse the network’s state of objects (that is known as marshalling).

ObjectOutputStream class

Use the ObjectOutputStream class to write Java objects and primitive data types to an OutputStream. Only java.io-compatible objects are accepted. The Serializable interface allows for the writing of streams.

ObjectInputStream class

An ObjectInputStream deserializes objects and primitive data that have been written to an ObjectOutputStream.

Java Deserialization

Deserialization is the process of reassembling an object from its serialized state. It is the reversal of the serialization operation.

The JVM makes an attempt to revive a deserialized object by constructing a new object on the heap in the same state as the serialized object at the time of serialization. except for transitory variables, which have default primitive values or return null (for object references).

Transient

You can mark any data member of a class as transient if you don’t want to serialize it.

Now, the id will not be serialized, so you will not get the value of the id when you deserialize the object after serialization. It will always return the default value. Because the data type of id is an integer, it will return 0.

SerialVersionUID

The SerialVersionUID is an id that is associated with each Serializable class at runtime by the serialization process. It is utilized to verify the sender and receiver of the serialized object. The same individual must be both the sender and the recipient. It is validated using SerialVersionUID. When the object is deserialized, an InvalidClassException is raised if the sender and receiver do not share the same SerialVersionUID.

Get Nimesh Mendis’s stories in your inbox

Join Medium for free to get updates from this writer.

We can also declare our own SerialVersionUID in the Serializable class. Create a field called SerialVersionUID and enter a value in it to accomplish this. It must be a lengthy process that yields static outcomes. It is advised to explicitly declare and set the serialVersionUID field to private in the class.

BULLET POINTS

  • You can save an object’s state by serializing the object.
  • To serialize an object, you need an ObjectOutputStream (from the java.io package)
  • Streams are either connection streams or chain streams
  • Connection streams can represent a connection to a source or destination, typically a file, network socket connection, or the console.
  • Chain streams cannot connect to a source or destination and must be chained to a connection (or other) stream.
  • To serialize an object to a file, make a FileOuputStream and chain it into an ObjectOutputStream.
  • To serialize an object, call writeObject(theObject) on the ObjectOutputStream. You do not need to call methods on the FileOutputStream.
  • To be serialized, an object must implement the Serializable interface. If a superclass of the class implements Serializable, the subclass will automatically be serializable even if it does not specifically declare implements Serializable.
  • When an object is serialized, its entire object graph is serialized. That means any objects referenced by the serialized object’s instance variables are serialized, and any objects referenced by those objects…and so on.
  • If any object in the graph is not serializable, an exception will be thrown at runtime, unless the instance variable referring to the object is skipped.
  • Mark an instance variable with the transient keyword if you want serialization to skip that variable. The variable will be restored as null (for object references) or default values (for primitives).
  • During deserialization, the class of all objects in the graph must be available to the JVM.
  • You read objects in (using readObject()) in the order in which they were originally written.
  • The return type of readObject() is type Object, so deserialized objects must be cast to their real type.
  • Static variables are not serialized! It doesn’t make sense to save a static variable value as part of a specific object’s state, since all objects of that type share only a single value — the one in the class.
  • To write a text file, start with a FileWriter connection stream.
  • Chain the FileWriter to a BufferedWriter for efficiency.
  • A File object represents a file at a particular path, but does not represent the actual contents of the file.
  • With a File object you can create, traverse, and delete directories.
  • Most streams that can use a String filename can use a File object as well, and a File object can be safer to use.
  • To read a text file, start with a FileReader connection stream.
  • Chain the FileReader to a BufferedReader for efficiency.
  • To parse a text file, you need to be sure the file is written with some way to recognize the different elements. A common approach is to use some kind of character to separate the individual pieces.
  • Use the String split() method to split a String up into individual tokens. A String with one separator will have two tokens, one on each side of the separator. The separator doesn’t count as a token.

--

--