What is the difference between serialized and non-serialized objects?

7

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

    
asked by anonymous 11.10.2014 / 10:41

1 answer

5

Serialization means to get an object (or set of objects) and place it in an appropriate format for streaming it or saving it on file. Objects that implement Serializable are not serialized, they are able to serialize .

At first, it is possible to serialize objects in binary or text format (XML, JSON, etc). I do not know which forms Java supports natively, but the main one is a binary language format. Serialization is typically done through ObjectOutputStream (to save a 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 be able to serialize, at first you just have to implement this interface and Java takes care of "magic". However, if for some reason you need your own logic to handle 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 references another, serialization also writes it to the stream (and any others it references, against). So if you do not take care you can end up with a gigantic file with objects that you wanted and others you did not want to serialize. One way to tell Java that the X field is not to be serialized is through the transient modifier. 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 B class is also Serializable , or the serialization / de-serialization process throws an exception ( C does not matter if it is or not, since the reference to it is transient ).

    
11.10.2014 / 11:16