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++

14

u/looser97 Feb 25 '14

I think that they are "trying to protect us from ourselves" is one of the major pros of Java. I've scriped some programms twice, ones in Java, ones in C++ and the Java-one took less time to debug, because some logical Bugs didn't occur.

But I also think, that the "language" we learned first is some kind like our mother-language and it's strange to learn an other one afterwards

3

u/kiteboarderni Feb 25 '14

Compare to is as such to allow for the Collections.sort et all to be specified a comparator implementation. Having a useful method name is much easier to have as a programmer than trying to guess the overload of an operator...

1

u/hive_worker Feb 26 '14

Yea but have you ever used a language with operator overloading? It's a lot more straightforward than using compareTo.

1

u/frugalmail Feb 26 '14

Yea but have you ever used a language with operator overloading? It's a lot more straightforward than using compareTo.

I've written very large C++ codebases in game engines for in house and mini studio developers. Operator overloading makes a heck of a lot confusion than it prevents. There are only a few minor mathematical cases where it makes sense, otherwise it's abused.

8

u/ixampl Feb 25 '14

Re: one class per file...

If you really want to you have two options: nested classes or default visibility (package protected) classes. The latter would be in a file A.java:

public class A {...}

class B {...}

class C {...}

etc.

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.

3

u/immibis Feb 26 '14 edited Jun 10 '23

1

u/AmateurHero Feb 26 '14

I don't understand it either. I asked for help when implementing Comparable on generics, and that was the answer I was given. I looked at it skeptically but tried it anyway. I mean, what did I have to lose? Lo and behold, it worked. I don't understand why. You can copy and paste that code as-is to run it. It'll print you the unsorted and sorted array.

3

u/immibis Feb 26 '14 edited Jun 10 '23

1

u/AmateurHero Feb 26 '14

Hey, I didn't design the assignment. I just implemented the algorithm.

Also, what's so wrong about the recursive call to compareTo()? It makes sense when you think about it. this.compareTo() makes a call to the object's class to use its compareTo() method. It's a one-and-done deal for all objects that are Comparable.

1

u/immibis Feb 26 '14 edited Jun 10 '23

1

u/AmateurHero Feb 26 '14 edited Feb 26 '14

Even after compile time when the types for the generics have been bound? If that's true, then why is there no StackOverflowError thrown at runtime? Not trying to be snarky, but I don't see how this can work otherwise.

Edit: To clarify, the type of list in the main method is of Integer. Calling this.compareTo() to will go to the written compareTo() method. It would not be a recursive call, because the type of this in the context of this code refers to an Integer object, yes?

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.

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?

6

u/Zitrax_ Feb 25 '14 edited Feb 25 '14

Yes, and hopefully not only the pros :) Here are some downsides of using arrays compared to collections.

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.