1

I was in an interview where I was asked:

What is the difference between the generic implementation of ArrayList and the older implementation which does not utilize generics?

I know the difference in how they are used, advantages, and restrictions, but I can't find implementation differences.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

1

Bit of a weird question; presumably they are asking about, essentially, the 'diff' between the source file of ArrayList.java as it was in java 1.4 vs what it looked like in java 1.5.

The only difference is that all the various places where <> appear, they simply don't in the 1.4 version, and the types in a select few places have changed from Object in 1.4 to E in 1.5, but not as many as you'd think (for example, the backing array is still Object[], not E[], intentionally). get(int) used to read public Object get(int index) but now reads public E get(int index), and add used to read public void add(Object item) but now reads public void add(E item).

This seems esoteric and not particularly relevant, but usually interviewers are not neccessarily asking questions because they are looking for a specific answer; often they are asking questions just to see where it goes.

For example, if I was interviewing somebody and this confuses them and they get extremely flustered, that's important information (that you get flustered easily and are unsure of your skill levels).

It would also be interesting to me if someone immediately rattles off all the changes, though less so (that'd be 'the right answer', I guess).

It would also be interesting if the interviewee goes off on a tangent on generics, or on its implementation in other languages, or on the notion of backwards compatibility.

I tend to like technical answers. The most impressive answer I can think of is if someone would do a bit of a deep dive on why ArrayList still uses Object[] as the backing array and not E[], even though I specifically asked for 'the differences' (as that part isn't different, but because it works like that, e.g. the get() method has ugly-casts in it: Casts to a type var, which cause compiler warnings).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72