• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Array serialization Question

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, agreed, and its clear now. Thanks all for the reply.. C you soon with another question/doubt
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic