Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if we don't serialize object in Java?

As we all know Serialization in java is a mechanism of writing the state of an object into a byte stream.

It is mainly used to send object's state across the network. But what happens when object is not serialized and is sent across the network .

Will it give errors?

Thank you

like image 566
Kapil Devmurari Avatar asked Apr 19 '16 13:04

Kapil Devmurari


1 Answers

We use Object Serialization for mainly two reasons:
1. Communication over the network
2. Lightweight persistence–the archival of an object

Before I respond to what errors you will get, let me take a dig at

Why do we need Serialization?

The main drivers for this are our network infrastructure and the hardware disks that understands bits and bytes but not JAVA objects. Serialization helps to translate graph of Java objects into an array of bytes for storage or transmission. All this happens due to the ObjectInputStream/ObjectOutputStream classes, full-fidelity metadata, and the willingness of programmers to "opt in" to this process by tagging their classes with the Serializable marker interface.

What happens if you try to send non-serialized Object over network?

When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.

like image 130
The Roy Avatar answered Oct 30 '22 09:10

The Roy