etienno wrote:
> I need to explain that to my collegue (some returns Vector and
> ArrayList). I would like to explain them that returning a List is
> "better", but I cannot find much argument about it.... Yes, the
> implementation can change without changing the signature, but what
> else? Could someone gives me a better answer?
Using the interface instead of the implementation is better,
because of the reason you said. You can change the used
implementation without problem. And changing this happens,
it's not a only a theoretical contruct. Happened here and
will happen at your place as well.
If you change the signature of a method from e.g. java.util.LinkedList
to java.util.ArrayList (or vice versa) all applications using this
class (e.g. residing inside a jar) have to be recompiled otherwise
you will get NoSuchMethodErrors during runtime.
All that can be avoided when using java.util.List. BTW. To
avoid performance-issues it's better to iterate through
a list using
Iterator it myList.iterator();
while (it.hasNext()){
Object o = it.next();
// do something
}
than
for (int i = 0; i < myList.size(); i++){
Object o = myList.get(i);
// do something
}
Especially if you don't know the concrete implementation being
used. LinkedList ist quite slow when doing random access.
Regards, Lothar
--
Lothar Kimmeringer E-Mail:
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong
questions!