Today at school, I had to present a demo of a basic sort of game simulator – sounds great right? Well, not so much. The programming language had to be Java.

I try to avoid this language as much as I can but unfortunately schools think Java is the only language out there to teach OOP.

So I thought it would be a good occasion to rantˆWexplain why I hate Java.

Takes 10 LOC when other languages require just one

Java is verbose in the sense that you need to write a bunch of lines of code to achieve very basic things.

This is especially true with Swing and even the language syntax is redundant, like this line:

HashMap<String, Thing> allTheThings = new HashMap<String, Thing>();

Here’s an issue I ran into: there is no way to pick a random item from a collection in Java.

In Java (example from SO):

int size = myHashSet.size();
int item = new Random().nextInt(size);
int i = 0;
for(Object obj : myhashSet)
{
    if (i == item)
        return obj;
    i = i + 1;
}

In Ruby:

my_collection.sample

Can’t code without a bloated IDE like Eclipse or Netbeans

To mitigate the abovementioned point, IDEs help you typing all those pointless redundant lines of code thanks to “Generate getters/setters”, CTRL+Space and that kind of features.

But those IDEs have serious drawbacks:

  • Want to use them on small Eee PC 10” screens? You get a 200x150 px box left for your code, very handy…
  • Want to launch them in less than ten seconds? No way.
  • Want to type efficiently using a Vim-like behaviour editor? You can’t.
  • Want to perform a simple text search through all your codebase? You can’t, really?!

I spent two minutes looking for this feature in Eclipse with no luck. Either it’s seriously well hidden or simply missing! I ended up using ack

Everything is broken and nobody cares

The are heaps of flawed design decisions which have been made in the Java API. But it’s all right, no need to fix it. Let’s just stick with this shitty design for backward compatibility…

Observer

Want to use the Observer pattern for your Swing UI? Well, be aware that your class to observe can’t be inherited since Observable is a class and not an interface! And Java does not have multiple inheritance to help you here (though I don’t advocate multiple inheritance).

More on this on SO.

InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonPainter

No, this is not a crazy typo, look at the doc.

Seriously, how would you get it right the first time? Oh wait, CTRL+Space, we meet again.

There is a whole thread on it by the way.

Cloning an object

object.clone() just doesn’t work as it should and is strongly deprecated.

Date/time and calendar

Working with dates and time is messy and SimpleDateFormatter is convinced weeks start on Sunday and you can’t change it. But hey, it’s called “simple” for a reason I guess.

No heredoc

You end up using StringBuilder when you could avoid it.

Obscure errors

I just added a new static field in a class, compiled, run again and it all blew up in my face:

A fatal error has been detected by the Java Runtime Environment:

 SIGSEGV (0xb) at pc=0x0000000000000000, pid=24019, tid=140302240622336

JRE version: 7.0_09
Java VM: OpenJDK 64-Bit Server VM (23.2-b09 mixed mode linux-amd64 compressed oops)
Problematic frame:
C  0x0000000000000000

Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again

An error report file with more information is saved as:
/home/infertux/workspace/project/hs_err_pid24019.log

If you would like to submit a bug report, please include
instructions on how to reproduce the bug and visit:
  http://icedtea.classpath.org/bugzilla

What a lovely segfault! Why???

Not portable

XKCD 801

Java is supposed to be portable thanks to the great JVM.

This is a lie:

  • If your system (OS or architecture) doesn’t have a JVM, nothing runs ; which happens quite often with embedded hardware.
  • If your JRE is too old, it won’t work either.
  • It’s not really portable anymore once your app gets complex.

Not to mention encoding issues on user inputs from switching between Linux and Mac (and probably Windows I suppose).

User interface

Ugly

Applications in Java don’t use your system’s UI but this ugly Java interface that makes you feel like running on Windows 95.

Painful to code

Have you ever tried to make a decent UI with Swing? Then you should know what I’m talking about.

Security

Last but not least (I left this one for the end since it’s an obvious one).

Just today, I saw so many posts on Hacker News about recent critical exploits found in Java. Worst thing about it: Oracle doesn’t seem to care at all and no upcoming patches are announced…

Anything else?

Yeah sure, plenty of them. Those above are just the ones on the top of my head that I had to deal with for this tiny MVC project.

Here’s a few links.

That’s why I think Java is one of the worst programming languages ever and I avoid it like the plague. I wonder if there is polyglot programmers (who have tried other languages) who actually still enjoy to code in Java.