DISQUS

Enigma Curry: Python vs Java

  • Bas Scheffers · 2 years ago
    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!
  • ryan · 2 years ago
    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?
  • O · 2 years ago
    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.
  • Hans Granqvist · 2 years ago
    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! :)
  • ryan · 2 years ago
    @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.
  • mmx2 · 2 years ago
    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...
  • ryan · 2 years ago
    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<something> alist = new ArrayList</something><something>(Arrays.asList((1,2,3,4)));" .. IMO Python is much more inline with basic algrebraic notation than Java is.</something>
  • progma · 1 year ago
    бля. нашел что с чем сравнивать.
    ты б еще вспомнил C++ да с Бейсиком сравнил
  • Carlos · 1 year ago
    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);
    }
    }
  • Carlos · 1 year ago
    The code above is horrible, so I did an image
  • Carlos · 1 year ago
    Why I can't put links here?
  • Carlos · 1 year ago
  • ryan · 1 year ago
    Hi Carlos,

    The link feature works, you just need to put some text inside the and 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.
  • Carlos · 1 year ago
    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'.
  • Carlos · 1 year ago
    ps: how do I edit my post here? I have posted wrong website in previous post, lol
  • Henrique · 1 year ago
    Can't compare. Python is a multi-paradigm, VHLL with clean syntax. Java is an ugly mishmash of concepts and syntax.
  • Rastus · 1 year ago
    When I was required to learn Python for a new job in 2003, I had been coding in Java as my preferred language since Java 1.0 in 1995. I went down the Python path kicking and screaming every step of the way.

    Now, 5 years later, assuming that I had a choice (and I often do), I would choose Python over Java for all but the largest software projects ... say greater than 100,000 lines of Java code (40,000 lines of Python code). While there are exceptions where existing Java libraries provide a higher return on investment compared to Python, it takes a very large project for the general advantages of Java to become apparent.

    And I know hard core Python programmers who think I'm nuts to use Java in these cases as well.

    I love Python whereas Java is a decent meal ticket.
  • Carlos · 10 months ago
    Here I am again.

    Python is really very good, I had to learn it because of my job.

    Now I am testing Jython, its a python implementation that runs over JVM and can use Java API.
  • EnigmaCurry · 10 months ago
    Cool! Try and find a local user group, if you can. Meeting other people who use the language on a daily basis is both fun and enlightening.
  • robert · 2 months ago
    hi ryan,
    getting error on your python sample code : http://pastebin.com/m43daa46c

    >>>
    Traceback (most recent call last):
    File "/home/robert/Documents/python/p2.py", line 7, in <module>
    appleList = [Apple('golden delicious'),
    TypeError: this constructor takes no arguments
  • EnigmaCurry · 2 months ago
    Should be __init__ not __int__