Content-Type: RST .. _warned: **Warning**: This entry is really *long* and almost definitely requires programming experience to understand. So I told one of my friends that during my ride back to New York from DC I could probably get some sleep. She said she knows me better than that, and that I would most definitely not sleep the entire way. Turns out she's right, since I got a bee on my bonnet regarding language comparisons - *"beginner"* languages. The two most popular beginner languages of today are Java and C++, each to its different class: Java is more common in high school compsci classes because of the AP Computer Science exam being Java-based, and C++ is more common in colleges because of... I don't even know why. Probably because of how powerful a language it is and the amount of doors it opens. Of course, then there are the few rebel teachers_ who see that... .. _teachers: http://www.elkner.net .. sourcecode :: java public class{ public static void main(String[] args) { System.out.println("Hello, world!"); } } ... and... .. sourcecode :: cpp #include int main(){ cout << "Hello, world!" << endl; } ... may be obfuscating the point of a **simple** helloworld program, and they prefer Python's .. sourcecode :: python print("Hello, world!") These teachers' numbers are growing, especially since colleges are begining to adopt Python as their Programming 101 language simply because of the lesser amount of syntax needed, allowing the course to focus on programming concepts instead of whether to use a double quote or a single quote for a string. Okay, maybe I'm not qualified to talk about what these languages are like for a beginner, since none of them were my beginning language. Pascal was. .. sourcecode :: pascal program helloworld; begin writeln("Hello, world!"); end. But especially *because* none of them was my starting language, perhaps I can be impartial, right? Or maybe try to? A little? Okay, I'll just talk about what they were like for *me* then. Java was the first of these three that I learned. I encountered it in the `Accelerated Computer Science`_ course at my `high school`_, in the context of the JKarel robots. Oh, those were the days. .. _`Accelerated Computer Science`: http://academics.tjhsst.edu/compsci/CSweb/index.html .. _`high school`: http://www.tjhsst.edu .. figure :: http://blog.opensourcenerd.com/upload/hello-karel Hello, Karel! `source `__ I do have to commend Karel for being a really friendly little bugger to explain to me what the heck object-oriented programming is. Believe it or not, it was a fairly large paradigm shift going from the Pascal/BASIC/C mentality of primitive types only, organized in structs or my own weird data structures via arrays into a "class" which magically contained its own data and had its own methods and other properties. If it wasn't for Karel, though, Java would have just been a really inconvenient way for me to write procedural programs: having to wrap everything in a ``public class foo`` to make it go. I'm afraid this may have happened had I learned C++ instead, since it doesn't have a nice little Karel library (that I know of). I might just have stuck to the procedural way of doing stuff, only coding classes just because the teacher wanted me to, or some other stuff like that. .. figure :: http://blog.opensourcenerd.com/upload/confused-student-cat Can haz getCheesburger() metud? Python has a Karel-ish tool as well, called `Guido van Robot`_. I am not as familiar with it as opposed to Karel, because I just picked up Python as I went, but GvR was supposedly fun to code the backend for (or so I hear)? .. _`Guido van Robot`: http://gvr.sourceforge.net/ Oh, Python. You restored my faith in programming needing a human brain to not be a syntax checker and on *some* things making sense. I won't go into great depth, but take a look at some of this nonesnse I ran into in my Java course: .. sourcecode :: java public class foo{ public static void main(String[] args){ String a = "foo"; String b = "foo"; a += "bar"; b += "bar"; System.out.println(a + "==" + b + " ==> " + (a==b)); System.out.println(a + " equals " + b + " ==> " + (a.equals(b))); } } The output: .. sourcecode :: plaintext foobar==foobar ==> false foobar equals foobar ==> true What?! To someone who does not know what a memory address is, this makes very little sense. Plus, isn't Java supposed to be object-oriented? If you're comparing the equality of two *objects*, perhaps it shouldn't compare their references, but their values. And, while always using methods is a nice way to be organized, why do I need a special method for *comparison*? There are builtin operators for that. If I wanted to compare memory addresses, I'd use pointers. Or, not Java. Python got it right: .. sourcecode :: python a = 'foo' b = 'foo' a += 'bar' b += 'bar' print(a + "==" + b + " ==> " + str(a==b)) print(a + " is " + b + " ==> " + str(a is b)) The output: .. sourcecode :: plaintext foobar==foobar ==> True foobar is foobar ==> False C++ got it right, too, and in a less ambiguous way than Python. ``==`` compares memory addresses unless it's overloaded by the writer of the class to do something else. While this allows it to keep the low-level-ness of C, it also allows programmers to customize their classes to, well, act like objects are supposed to act. .. figure :: http://blog.opensourcenerd.com/upload/tell-me-when-its-over I `warned`_ you, didn't I? What am I saying? Java may be fine for a little while, but it gets unintuitive fast. Really fast. Say you wanted a mutable-size array. In Java? ``import java.util.ArrayList``. In C++? ``#include ``. In Python? There by default. That's fine, but how do you use these? In each of the cases below, `a` is one of these arrays. .. sourcecode :: java // Java a.get(pos); // getting an element a.set(pos, some_object); // setting an element a.add(some_object); // adding an element to the end a.add(pos, some_object); // adding an element at position pos a.addAll(b); // adding all elements of another array a.size(); // how many elements are there? .. sourcecode :: cpp // C++ a[pos]; // getting an element a[pos] = some_object; // setting an element a.pushBack(some_object); // adding an element to the end a.insert(a.begin()+pos, some_object); // adding an element at position pos a.insert(a.end(), b.begin(), b.end()); // adding all elements of another array a.size(); // how many elements are there? .. sourcecode :: python # Python a[pos] # getting an element a[pos] = some_object # setting an element a.append(some_object); # adding an element to the end a.insert(pos, some_object) # adding an element at position pos a += b # adding all elements of another array len(a) # how many elements are there? Java is all methods, C++ is long-winded and weird with its iterators, and Python is short and sweet. But, honestly: ``a.get(pos)``? And, while C++'s versions of the add and addAll commands may be rather opaque to those who don't know iterators, iterators are much more powerful than anything Java has. Let's try to sort that array we got, something many beginner programmers may want to do. .. sourcecode :: java // Java Object[] ca = a.toArray(); // XXX: errors/warnings! Arrays.sort(ca); for (int i=0; i // ... sort(a.begin(), a.end()); .. sourcecode :: python # Python a.sort() Java is obscene. In this example, Python may seem to have the upper hand, but it really doesn't: the sort() method is builtin to the list class, and if I were to, for example, make a LinkedList class, or something else that's not a simple flat list, I'd be hard pressed sorting it without something like C++'s iterators. For a beginner, though? C++ can make just as much sense as Python with a little explanation. Okay, so I don't like Java. So what? Well, I'm trying to maybe get the word out that *it is not all that good of a beginner language*! Many students only take an intro CS course, and if they learn Java, they really aren't left with something usable later. C++ either, because both Java and C++ take extra effort to get a project or script off the ground. Python is instant-gratification, though, and even a biologist might find it useful to write a small script to process a large data file or the likes. And, for those who go on to bigger and better compsci things, both C++ and Python offer large libraries and sets of cool tools for everything from OpenGL to web development to GUIs to XML parsing, etc. Not that Java doesn't but it's at the midway between "harder, but efficient and powerful language" and "sluggish, but easy and tool-packed language", stuck somewhere at "rowing a boat in a lake of runny molasses". .. figure :: http://blog.opensourcenerd.com/upload/failboat The bottom line, though, is that in the selection of beginner languages you can do much better than Java. I personally like the way classes here at NYU-Poly are structured: CS 111 is Python, CS 112 is C++, then advanced courses follow. You can do much worse than Java, too. I feel bad for the poor student somewhere whose first language was Lisp. Or Brainfuck_. .. _Brainfuck: http://en.wikipedia.org/wiki/Brainfuck