Why I hate Java (part 1)

People have often asked for my opinion on Java (the programming language, not the Runtime engine, or the Coffee), and my response is normally a very unreserved expression of hatred. I have condensed all of my evidence from the past year, and the result is the following piece of code:

 public class Dodgy{ public static void main (String[] args) { boolean a = true; boolean b; if (a == false) { b = true; } if (a == true) { b = false; } System.out.println(b); } } 

Whilst this code looks harmless, let’s see what Java does when I try to compile it:

chris@blackboard:~/src$ javac Dodgy.java Dodgy.java:14: error: The local variable b may not have been initialized System.out.println(b); ^ 1 problem (1 error) 

It is obvious, from such a trivial example, that b will be initialised when line 14 is reached, however, Java prevents my code from even compiling. This demonstrates one differentiation between Java and C (or C++): Java does not trust your judgement. With appropriate syntax changes, that code would have been valid C code, but in Java it is illegal.

Whilst I would approve of such a case being a compiler Warning (I could then flag a -Wall or similar, and have my compiler abort when it finds such an issue), but causing such a thing to be an error is irrational.

Of course, all cases of whether a variable is initialised or not cannot be verified: a general purpose algorithm to do such a thing can be proven to not exist via reduction to the halting problem; so why should Java even attempt to do it in the first place?

So there you have it: Java has a distinct lack of trust in the programmer who uses it, and this is why I dislike it.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>