What’s the difference between serialized and non-sserialized objects?

Asked

Viewed 340 times

6

I know an object is serialized when its class implements the interface java.io. but I would like to know what it means that the object is serialized and what difference there is with another not serialized.

  • 1

    http://1.bp.blogspot.com/-mPfJToWysdk/Umjnp5_ut3i/AAAAAAAGE/Cm2ncoaxdcm/s1600/serialization.png

1 answer

4

Serialization means to take an object (or set of objects) and put it in an appropriate format to transmit it in network or save it in file. Objects that implement Serializable nay are serialized, they are capable of serialize.

At first, it is possible to serialize objects in binary or text format (XML, JSON, etc). I don’t know which forms Java supports natively, but the main one is a binary format proper to the language. Serialization is typically done through ObjectOutputStream (to save an object in a stream) and ObjectInputStream (to read a stream with serialized data and "reassemble" the object).

An example (save objects to file, then read them back):

  /* Escrevendo alguns objetos num arquivo */
  FileOutputStream fos = new FileOutputStream("t.tmp"); // stream de arquivos, normal
  ObjectOutputStream oos = new ObjectOutputStream(fos); // decorador para salvar objetos

  oos.writeInt(12345); // tipos primitivos são serializáveis
  oos.writeObject("Today"); // Strings também
  oos.writeObject(new Date()); // a classe Date implementa Serializable

  oos.close();

  /* Lendo esses mesmos objetos do arquivo */
  FileInputStream fis = new FileInputStream("t.tmp");
  ObjectInputStream ois = new ObjectInputStream(fis);

  int i = ois.readInt();
  String today = (String) ois.readObject();
  Date date = (Date) ois.readObject();

  ois.close();

If you want your classes to also be able to serialize themselves, at first just implement this interface and Java takes care of the "magic". However, if for some reason you need your own logic to deal with your objects, you can do this by implementing the methods in the class:

private void writeObject(java.io.ObjectOutputStream stream)
    throws IOException;
private void readObject(java.io.ObjectInputStream stream)
    throws IOException, ClassNotFoundException;
private void readObjectNoData()
    throws ObjectStreamException;

Check the Java documentation for more details on how they work.

Finally, it should be noted that if one object refers to another, serialization also writes in stream (and anyone else he refers to, and so on). So if you’re not careful you can end up with a gigantic file, with objects that you wanted and others that you didn’t want to serialize. A way to inform Java that the X field is not to be serialized is through the modifier transient. Example:

class A implements Serializable {
    private B campoPermanente; // Será serializado
    private transient C campoTemporario; // Não será serializado, voltará como null

In this example, it is important that the class B also be Serializable, or the process of serialization/de-serialization will make an exception (C no matter if it is or not, since the reference to it is transient).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.