Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array serialization Question

 
Anupam Bhatt
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anupam Bhatt
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, agreed, and its clear now. Thanks all for the reply.. C you soon with another question/doubt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic