views:

154

answers:

6

Sometime back our architect gave this funda to me and I couldn't talk to him more to get the details at the time, but I couldn't understand how arrays are more serializable/better performant over ArrayLists.

update:This is in the webservices code, if it is important and it can be that he might mean performance instead of serializability.

Update: There is no problem with XML serialization for ArrayLists.

  <sample-array-list>reddy1</sample-array-list>
  <sample-array-list>reddy2</sample-array-list>
  <sample-array-list>reddy3</sample-array-list>

Could there be problem in a distributed application?

+1  A: 

They both serialize the same data. So I wouldn't say one is significantly better than the other.

Bozho
@Bozho - *"they both serialize the same data"* - not exactly ... see my answer.
Stephen C
+6  A: 

There's no such thing as "more serializable". Either a class is serializable, or it is not. Both arrays and ArrayList are serializable.

As for performance, that's an entirely different topic. Arrays, especially of primitives, use quite a bit less memory than ArrayLists, but the serialization format is actually equally compact for both.

In the end, the only person who can really explain this vague and misleading statement is the person who made it. I suggest you ask your architect what exactly he meant.

Michael Borgwardt
"Both arrays and ArrayList are serializable." If the types they contain implement `Serializable` that is.
R. Bemrose
@R. Bemrose: true, that's of course also a prerequisite.
Michael Borgwardt
The overhead for first serialised object will be somewhat higher for an `ArrayList`.
Tom Hawtin - tackline
@Tom - Can you explain more?
Reddy
note that the backing array in `ArrayList` is transient, and it is serialized in the `writeObject` method.
Bozho
@Reddy There needs to be a descriptor for the `ArrayList` class which names the class, the fields, the types of the field, etc. If there isn't much data, then that can be a relatively large proportion of the message.
Tom Hawtin - tackline
Understood.. thanks Tom
Reddy
+3  A: 

Maybe he was refering to XML-serialization used in Webservices ? Having used those a few years ago, I remember that a Webservice returning a List object was difficult to connect to (at least I could not figure it out, probably because of the inner structure of ArrayLists and LinkedLists), although this was trivially done when a native array was returned.

To adress Reddy's comment,

But in any case (array or ArrayList) will get converted to XML, right?

Yes they will, but the XML-serialization basically translated in XML all the data contained in the serialized object.

For an array, that is a series of values. For instance, if you declare and serialize

int[] array = {42, 83};

You will probably get an XML result looking like :

 <array>42</array>
 <array>83</array

For an ArrayList, that is :

  • an array (obviously), which may have a size bigger than the actual number of elements
  • several other members such as integer indexes (firstIndex and lastIndex), counts, etc

(you can find all that stuff in the source for ArrayList.java)

So all of those will get translated to XML, which makes it more difficult for the Webservice client to read the actual values : it has to read the index values, find the actual array, and read the values contained between the two indexes.

The serialization of :

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(42);
list.add(83);

might end up looking like :

<firstIndex>0</firstIndex>
<lastIndex>2</lastIndex>
<E>42</E>
<E>83</E>
<E>0</E>
<E>0</E>
<E>0</E>
<E>0</E>
<E>0</E>
<E>0</E>
<E>0</E>
<E>0</E>

So basically, when using XML-serialization in Webservices, you'd better use arrays (such as int[]) than collections (such as ArrayList<Integer>). For that you might find useful to convert Collections to arrays using Collection#toArray().

Axel
Yes, this is in the web services. But in any case (array or arraylist) will get converted to xml ...right?
Reddy
I have used ArrayList, HashMap in the response and checked the XML. There are no extra fields for ArrayList, and HashMap is coming as empty tag. <sample-array-list>reddy1</sample-array-list> <sample-array-list>reddy2</sample-array-list> <sample-array-list>reddy3</sample-array-list> <sample-hash-map/>
Reddy
Huh, funny. I guess things might have changed since then, or maybe my memories are not accurate enough... I'll give it a shot when I get a chance to use NetBeans and see what kind of XML comes up... I'll edit my answer then if need be.
Axel
hmmm.... okay, thanks Axel.
Reddy
+3  A: 

I'm assuming that you are talking about Java object serialization.

It turns out that an array (of objects) and ArrayList have similar but not identical contents. In the array case, the serialization will consist of the object header, the array length and its elements. In the ArrayList case, the serialization consists of the list size, the array length and the first 'size' elements of the array. So one extra 32 bit int is serialized. There may also be differences in the respective object headers.

So, yes, there is a small (probably 4 byte) difference in the size of the serial representations. And it is possible that an array can be serialized / deserialized slightly more quickly. But the differences are likely to be down in the noise, and not worth worrying about ... unless profiling, etc tells you this is a bottleneck.

EDIT

Based on @Tom Hawtin's comment, the object header difference is significant, especially if the serialization only contains a small number of ArrayList instances.

Stephen C
hah, yes. That's because of the additional "capacity" option of the array list, which makes it needed that both the size and the length of the array are serialized. But we'd agree this is insignificant :)
Bozho
*"But we'd agree this is insignificant"* Absolutely!
Stephen C
A: 

Only in Java does this make a difference, and even then it's hard to notice it.

If he didn't mean Java then yes, your best bet would most likely be asking him exactly what he meant by that.

Eton B.
Even in C#.Net it does .
Bhaswanth
+1  A: 

As of i know,both are Serializable but using arrays is better coz the main purpose of implementing the ArrayList is for internal easy manipulation purpose,not to expose to outer world.It is little heavier to use ,so when using in webservices while serializing it ,it might create problems in the namespace and headers.If it automatically sets them ,you ll not be able to receive or send data properly.So it is better to use primitive arrays .

Bhaswanth
I disagree. There's nothing wrong with exposing Lists instead of arrays ... if that's going to be more convenient for whatever is using the API.
Stephen C
Bhaswanth, Can you explain "it might create problems in the namespace and headers"?
Reddy