2

Exploring Java classes, I realized that value classes such Date AtomicInteger, AtomicLong, BigDecimal, BigInteger, Byte, Double, Float, Integer, Long and Short should implements Serializable.

Could you help me to understand why all these classes should implements the marker interface Serializable?

Jesus Zavarce
  • 1,729
  • 1
  • 17
  • 27

2 Answers2

4

Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

Adding Serializable marks them having exactly the described functionality.

Example use of serialisation:

The serializability allows you to stream your class in a file and read it again from the file.

Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object.

public final void writeObject(Object x) throws IOException
public final Object readObject() throws IOException, ClassNotFoundException

These methods allow you to serialize and deserialize a class that is marked as serializable.

Additional information:

As mentioned by @Basil Bourque - when you read the documentation about a.e. BigInteger you find this:

static BigInteger ONE //The BigInteger constant one.
static BigInteger TEN  //  The BigInteger constant ten.
static BigInteger ZERO //The BigInteger constant zero.

source: JavaDocs

Compared to Integer the BigInteger is not a single primitve object. It rather consists of several objects. Together they behave like a primitive Integer. Except if a serializer meets this object it does not know if the class is serializable (No true primitive type). This you can handle by manually marking the class as Serializable by adding the implements Serialiable.

SWiggels
  • 2,159
  • 1
  • 21
  • 35
  • Serialization to a file using Java serialization is maybe not the best use case as Java serialization is not intended for long-term persistence. A common use case would be eg. remote EJBs, where your DTOs have Date, BigInteger,... fields. – Puce Sep 03 '15 at 09:05
  • @SWiggels Very good Answer. But I suggest you expand a bit about the specific classes mentioned in the Question. I suspect the questioner might be thinking that these simpler objects with seemingly a single value might be expected to serialize out without the Serializable. So you could mention that there is not necessarily a single value (BigInteger has multiple fields at least). And explain that while relatively simple when compared to our business objects, they still go through same handling process including a way to override their format when outputting & override their handling on input. – Basil Bourque Sep 06 '15 at 18:46
2

These are the basic and most using data types which should white list to transfer/preserve data. And the programmers do not have access to modify them by default they made of seizable.

Not only these there are many other Classes which are by default serializable. For ex : ArrayList

Read more about Serializable Objects

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307