Python vs Java

April 20, 2007 at 11:24 pm | In Java, Python |

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:

Python vs Java

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.


15 Comments »

RSS feed for comments on this post. TrackBack URI

  1. Just give in get an IDE designed for Java. I agree that Java is very verbose, but using IDEA, I can code in it faster than any of the other languages I use. (C, C++, Tcl)

    As for readability: as with any language that comes with experience; I have to strain quite hard to read you python example!

    Comment by Bas Scheffers — May 1, 2007 #

  2. Bas,

    You're right of course, an IDE can help a lot. I've got my emacs snippets working pretty nicely now. One of them expands "print" to "System.out.println();" which works quite nicely for my python trained fingers.

    As far as actually using something other than emacs… I can make the switch from python to java if I have to… but I don't think I can handle giving up my emacs :)

    Seriously though, I have tried Eclipse but I can't even figure out how to do a simple macro. I use macros at least once an hour with Emacs. Can IDEA do macros?

    Comment by ryan — May 1, 2007 #

  3. Wow, you have no conception of the roots of OO.

    Smalltalk ftw?

    Me: Where did I talk about the history of Object Orientation? I realize that Smalltalk came before both Java and Python, but that has no bearing on the comparison at hand.

    Comment by O — May 2, 2007 #

  4. Yeah, Java is verbose, but your examples are not equal. The Java example has an extra class, ListExampleClean, which adds two lines to the Java code.

    BTW, python doesn't have "list.remove(list)"? I guess you'd have to use set differences, which actually sort of makes sense.

    Nice indentation! :)

    Comment by Hans Granqvist — May 3, 2007 #

  5. @Hans Granqvist

    Yes, there are two classes. However, Java requires the main function to be wrapped in a public class. This is loosley analogous to the "if __name__ == '__main__':" in python.

    However, it does appear that I have some problems with my java code.. I'll upload a remake later today.

    Comment by ryan — May 3, 2007 #

  6. [...] Referência: EnigmaCurry [...]

    Pingback by Python vs Java « Terramel — July 12, 2007 #

  7. In all fairness, I've not coded anything in python for a long time now (used to create small games with pygame). But Java code (at least in your example) is far more readable than Python's code. Maybe it is because I am so rusty with python, or maybe java indeed is a much more clear language. Having less lines of code does not necessarily makes a code more efficient or easier to read and this example proves just that. In my honest opinion as a non-prof programmer, that is…

    Comment by mmx2 — September 13, 2007 #

  8. mmx2:

    "Dick and Jane" is also easier to read than an essay written by Carl Sagan. Readability only counts when it's balanced with economy and expressiveness.

    I think at every level Java adds non essential vebosity to the code. IMO, the only line of python that is less readable than java is "for x in someApples: appleList.remove(x)" .. that line only exists because python can't do set operations on lists. If this example had been using the native set class instead it would have been "appleList.difference(someApples)" which is on par with java's "appleList.removeAll(someApples)"

    Most people who are unfamiliar with programming languages are usually at least familiar with basic algebra. In algebra you would usually write things such as "x=4" rather than "Integer x = 4". Also for matrices (essentially the mathematical representation of a list or table) is easily represented as something like "alist = [1,2,3,4]" instead of "List alist = new ArrayList(Arrays.asList((1,2,3,4)));" .. IMO Python is much more inline with basic algrebraic notation than Java is.

    Comment by ryan — September 13, 2007 #

  9. бля. нашел что с чем сравнивать.
    ты б еще вспомнил C++ да с Бейсиком сравнил

    Comment by progma — June 23, 2008 #

  10. 16-line version of your program:

    import java.util.*;
    public class Apple {
    private String type;
    public Apple(String type) { this.type = type; }
    public String toString() { return type; }
    public static void main(String[] args) {
    String[] appleTypes = {"golden delicious", "gala", "granny smith", "fuji", "red", "braeburn"};
    ArrayList appleList = new ArrayList();
    for (String type : appleTypes) appleList.add(new Apple(type));
    System.out.println("appleList: " + appleList);
    List someApples = new ArrayList(appleList.subList(0, 3));
    System.out.println("someApples: " + someApples);
    appleList.removeAll(someApples);
    System.out.println("appleList: " + appleList);
    }
    }

    Comment by Carlos — August 31, 2008 #

  11. The code above is horrible, so I did an image

    Comment by Carlos — August 31, 2008 #

  12. Why I can't put links here?

    Comment by Carlos — August 31, 2008 #

  13. Hi Carlos,

    The link feature works, you just need to put some text inside the <a> and </a> tags. I fixed it for you above.

    Your code is certainly shorter than mine, but it seems quite obtuse to me. Would you actually write Java code that way? It seem not very OO IMHO.

    Thanks for dropping by.

    Comment by ryan — September 3, 2008 #

  14. Hello Ryan.

    Well, I agree with u that Python does more in less lines of code. At least in this case I think I cant reduce the code, unless I put everything in 1 line, but that would be completely horrible, lol. In your case u can reduce even more ur lines of code (ie all apples in 1 line), so I must respect that.

    About OO, my code is following OO rules. Maybe it is strange because of the main() method inside the same Apple class, but that is not a problem, u can use the Apple as a normal object. The only difference is that it has a main() method. If u want, u can add main() in every class to do some tests or to have an example of how to use your classes. But u must remember that by convention the class that will run your application must be called 'MainClass'.

    Comment by Carlos — September 4, 2008 #

  15. ps: how do I edit my post here? I have posted wrong website in previous post, lol

    Comment by Carlos — September 4, 2008 #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress.
Entries and comments feeds. Valid XHTML and CSS. ^Top^
XML Sitemap