On Java
I recently spoke with a friend of mine about his studies, and during the course of the discussion was amused by a perfect example of something I had vaguely suspected.
A bit of background: my friend and I both work in the Physics Department on gamma-ray telescope simulations. His are in space, mine or on the ground1. I had learned C++ in high school, and then Java during my Freshman year of college. My friend taught himself C++ last summer in order to work on his simulation software, and he's now taking an introductory Java class for his math minor.
I've noticed that in recent years there is a strong trend to switch to teaching introductory courses in Java. There are several advantages I can think of:
- C like syntax; it's not odd or really esoteric
- Java makes making a GUI really easy; it's part of the language rather than an external library. This is good for beginners, since drawing pretty pictures is way more fun than designing a command-line fast-food cash register2.
- More minorly, Java is (supposedly) portable
However, this trend just hasn't sat well with me; when it comes right down to it, I don't like Java much. Particularly, I don't like using a language in which the designers deliberately removed important and useful features like operator overloading because they though I might abuse them. Still, I couldn't think of any good argument for why Java was actually a poor first language to teach to beginners. My friend, however, did.
Suppose you want to write the canonical beginning program, Hello World. In C++ it looks like:
#include <iostream>
int main()
{
std::cout << "Hello world!";
return(0);
}
There's plenty going on in that code snippet, but it's not hard to explain to a total beginner what each part does: "What's 'include iostream?'" "That part tells the compiler to load the library for printing things to the screen." "What's int main?" "main is a label that tells the computer where to start. Int means when the program is done it's result will be and integer." And so on. Then, there's the Java version:
class HelloWorldApp {
public static void main(String[] args) {
System.out.print("Hello world!");
}
}
The Java version has fewer lines, but it's a lot more dense. What's really important, though is that it's far less clear how the Java version works. Particularly, Java's restriction that functions may not exist outside of classes means that you must create a class just to print "Hello world!" a single time, and it's much more difficult to adequately explain what a class is and why you need one to someone who's never programmed before. This was exactly what my friend pointed out to me; form having studied C++ before he already knew about classes, objects, and object oriented design, but most of his classmates did not. Apparently the professor had to literally say something along the lines of, "Uh, I can't ell you what those lines are yet, we'll learn that later." Likewise, compare this C++ tutorial to this Java tutorial. Note that the C++ page has a reasonable description of the entire program in a single paragraph, while the Java page, even despite going through the structure of writing a class definition still has to say:
Chapter 2 provides an overview of classes in general, and Chapter 4 discusses classes in detail. For now it is enough to know that every application begins with a class definition.
Even taking into account the fact that the Java page has sizable section headers, repeats the entire code listing multiple times, and discusses some finer details like comments, it's still significantly longer and more involved. This is, I think, unavoidable, and it presents a major disadvantage to using Java as a language for beginners: Java has significantly more syntactic and conceptual overhead before one can do anything at all.
While the advantages of beginning with Java remain, I remain unconvinced that it is a clear winner for such usage, and I really wonder whether or how thoroughly instructors have considered its pros and cons before making it the de facto standard.
I am a UW student and stumbled across your blog while searching for the Bradford Dissolvable Agent. I noticed that you don't have many comments and I thought I'd let you know that I appreciate your blog. It's the first blog I will ever subscribe to because I think we are very similar and you muse on things I find interesting. I look forward to seeing what you will write next.