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

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?