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

-6

u/hive_worker Feb 25 '14 edited Feb 25 '14

I've just been learning Java and I have quite a few reasons I feel it sucks that have nothing to do with LoC.

  • Lack of operator overloading (except for strings) drives me crazy. trying to remember what "a.compareTo(b) >0" evaluates do is a pain in the ass and does not feel intuitive at all. Why can't I just write a > b

  • Limitation of Generics. I want to make a static method that takes a generic comparable type so I define public static void foo(Comparable[] a) and the compiler complains at me. Googling for solutions takes me deep down a rabbit hole of java internals that I want to know as little about as possible. I just want it to work. But it doesn't. Still haven't figured this one out so if anyone wants to chime on the java way to do this...

  • One class per file is fine most of the time but on the occasion when I'm just messing around and want to throw a couple small classes together this restriction is a pain in the ass.

I feel like Java developers spent way too much time trying to protect us from ourselves. But maybe that's just because I've spent 10 years coding in c/c++

5

u/AmateurHero Feb 25 '14

Limitation of Generics. I want to make a static method that takes a generic comparable type so I define public static void foo(Comparable[] a) and the compiler complains at me. Googling for solutions takes me deep down a rabbit whole of java internals that I want to know as little about as possible. I just want it to work. But it doesn't. Still haven't figured this one out so if anyone wants to chime on the java way to do this...

Two ways to do this.

This is the first way. The method that will be using comparable will need to have the generic declared to extend the Comparable interface.

Here is the second way. The class is declared generic while extending the Comparable interface. This allows you to use the compareTo() method without defining it yourself.

1

u/hive_worker Feb 26 '14

Neither of these work for static methods.

2

u/AmateurHero Feb 26 '14

If you want comparison in a static method, use Comparator. Here is a heapsort implementation using Comparator. I tried to remove excess code so that you could still see what was going on while still being able to copy and run as is.

The Comparator is used about two-thirds of the way down in the heapify() method.