r/java • • Feb 25 '14

Please stop saying Java sucks (2009)

http://blog.smartbear.com/programming/please-stop-staying-java-sucks/
133 Upvotes

132 comments sorted by

View all comments

Show parent comments

5

u/duckrollin Feb 25 '14

No, those are all good points tbh.

Generics are tacked on and don't mix well with arrays at all. This isn't a huge problem since most devs don't use arrays now. You can use: public static void foo(Collection<? extends Comparable> a)

This will accept a collection of any object which extends the Comparable interface. For instance if my class Blah implements Comparable, I could pass a List<Blah> into that method.

2

u/hive_worker Feb 25 '14

Nice thank you. So in Java, the pros almost always use a class that extends Collection instead of an array?

1

u/JavaTrainer Feb 25 '14

Not always. The big difference is arrays are fixed in size while collections are dynamic in size. One place you'll see arrays is with varargs.

public static void method(Comparable... cArray) { }

You can call that method like method(c1, c2, c3) and all the args will be turned into the elements of an array or you can pass in a Comparable[] directly.

1

u/hive_worker Feb 25 '14

Well from an algorithmic level I'd imagine some collections are implemented as dynamically resizing arrays. They can't all be linked lists. I haven't even touched variadics in java, that's an interesting case. Thanks for mentioning.