Hi all,

I'm learning Serialization in JAVA .
I learned that "an array object is serializable as long as the things in the array are serilizable" .

I just need a clarification on that statement .

To understand that concept I myself coined a simple program .

Here it's :

import java.io.*;

public class Saveit {
   public static void main(String[] args) {
         int[] arr ={10,20,30,40,50};
        try {
               ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(new File("saveInt.ser")));
         }catch(IOException ex) { ex.printStackTrace() ; } 
    arr =null;
   try {
          ObjectInputStream is = new ObjectInputStream(new FileInputStream(new File("saveInt.ser")));
           int[] restore = (int[]) is.readObject();
           for(int i:restore) 
        catch(Exception ex) { ex.printStackTrace(); }

What I understood from that statement and program are follows :

1.We don't need to explictly implement Serializable for to save arrays
2.If we try to serialize an array it's contents should also be serializable(for objects only)
3.Primitive types are serializable by default if it is an array.

please clarify me what I understood is correct or not and also anything else I should know or missed.
Did I have misunderstood something ?

Thanks in advance

9 Years
Discussion Span
Last Post by parthiban

An array is just an ordered list of things. So for the array to be serializable the elements of the array must be serializable. Hence, your conclusions 1 is correct. Conclusion 2 is correct for all elements in an array (not just objects).

However, conclusion 3 needs a little help: Primitive types are serializable by default whether or not they are stored in an array. Therefore it follows that any array made of primitive types is also serializable.

Hope this helps.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.