aspose file tools
The moose likes Beginning Java and the fly likes Array serialization Question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Array serialization Question" Watch "Array serialization Question" New topic
Author

Array serialization Question

Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
Hi,

Another quick question.

Consider an array with "non-serializable" objects as elements.

I do understand that arrays are serializable, but wondering what happens if the objects it stores do not implement serializable interface? Can the array be still serialized?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19214

No it can't.

When serializing objects that hold references to other objects (and that includes arrays and collections), those references should be either serializable as well or transient. Array elements aren't transient so they should be serializable for the array to be completely serializable.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
Great.. thanks for the clarification..

BTW any ideas on, what is so special about Arrays that they we made serializable by default? I mean most of the provided java classes are not serializable by design, so what is the thought behind making Arrays as serializable by default.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Most of the coomonly-used Java libraray classes do in fact implement Serializable. Look in the API for String, Integer, JFrame, etc, etc.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19214

Originally posted by Anupam Bhatt:
Great.. thanks for the clarification..

BTW any ideas on, what is so special about Arrays that they we made serializable by default? I mean most of the provided java classes are not serializable by design, so what is the thought behind making Arrays as serializable by default.

Probably just to have arrays (and most collections as well) not be a limiting factor in serialization. Imagine the troubles you would have to go to to serialize your object just because the array itself wouldn't be serializable? Every single time you would have to write the number of elements followed by the elements themselves when serializing serializing. When deserializing you would have to read the number first, then read that many objects.

For your information, that's exactly what ArrayList, LinkedList and HashSet do. HashMap writes the key and value instead of the element, but also the size first. So why reinvent the wheel?
Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
yeah, agreed, and its clear now. Thanks all for the reply.. C you soon with another question/doubt
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Array serialization Question
 
Similar Threads
Remote interface arguments
request attributes/properties
request attributes/properties
What is the Types for RMI/IIOP?
HFEJB: Why shippable values must be one of these....