Python vs Java
April 20, 2007 at 11:24 PM | categories: python, java | View Comments
I've been coding in Python for two years now. I've recently been reteaching myself Java because some projects I'm working on require it. So far, about the only fun thing about it has been setting up Emacs abbreviations to handle all of Java's verbose syntax. I am simply amazed at how verbose Java is! Just to illustrate my frustration I coded one of the exercises I was working on in Java as well as in Python:
Yes, both of these code samples do the exact same thing.
On the top is Python. 17 lines of very readable code. On the bottom, weighing in at 28 lines, is Java. Brackets and semicolons are everywhere -- Redundant type declarations and "new" object instantiations -- No syntactic sugar in sight except for the "+" operator that I'm not even allowed to overload myself!
I even think I'm being a bit generous to Java, in the way I've formatted the above code, I've only used a newline when I thought it adds to the readability. You'll note that I closed three whole blocks of code on line 23 instead of on separate lines. Yea, I could start doing this everywhere else to save space but that starts to make the readability of the code much worse than it already is.
Sure, Java is a fine language, it certainly has a long life still ahead of it and will continue to grow and mature... but coming down from a two year long 'Python high' and coming back to Java is.. well.. less than thrilling.
Update: The original java code had newbie-esque syntactical errors that prevented compilation as well as a much larger technical problem: when using an Array as the input for an ArrayList it changes the behaviour of the ArrayList such that it is fixed length meaning it no longer implements a removeAll() method (nor even an add() method, what's a list if you can't add items?!?) Wrapping a Array.asList inside of the ArrayList fixes it, but logically adds yet another line of code and further obfuscates things.