Aug 17, 2015

Basics Revisited | Arrays vs Collections

Sometimes, it is being asked in a Java interview, difference be Arrays and Collections and when would you either of two. Even experienced Java programmers may get confused for a time. So, idea behind the post is to revisit some of the basic differences between the two. Not much explanation required as most of them self explanatory.

Array Collection
Arrays are fixed in size. Collections are grow-able in nature, i.e., based on our requirement, we can decrease or increase the size.
With respect to memory, arrays are not recommended to use. With respect to memory, collections are recommended to use.
With respect to performance, arrays are recommended to use. With respect to performance, collections are recommended to use, when compared with Arrays.
Arrays can hold homogeneous datatype elements only. Collections can hold both homogeneous and heterogeneous elements.
There is no underlying data structure for arrays and hence readymade method support is not available. Every Collection class is implemented based some data structure. Hence readymade method support is available for most of the requirements.
Arrays can hold both primitive and Object types. Collection can hold only Object types.

Please post in the comments section for any query.

0 comments:

Post a Comment