Tuesday, January 25, 2011

My Students Laugh, HaHa, Laugh, at the HN Naysayers ;)

I'm gratified by the response my recent article Should I Still Learn Java? has received, especially at sites like Hacker News and DZone.

Among the comments at HN were some folks who felt that Java is a poor choice of language for beginning programmers. There were a number of reasons given, many of those reasons are pretty far removed from the beginner, I feel. Then there were some that trotted out that other old argument, the complexity of the "Hello, world!" program:

class HelloWorld{
public static void main(String[] arg){
System.out.println("Hello, world!");

It's an ugly-looking mouthful, I'll admit. But a complete obstacle to learning Java? Not even close.

If you're studying Java on your own, there's a lot in there that tends to get glossed over in texts on Java. In writing, it's hard to strike a balance between infodumps and magic code when you're trying to maintain a pace to the narrative of the book. I've broken down the elements of the basic program in several articles at my other blog A Beginning Programmer's Guide to Java, including the articles on the Basic Program Skeleton, Code Blocks, and main().

But how long does it take to cover this material in class? Can it be done without sending the students into the Land of Nod?

The Proof is In the Results

Actually, it's far easier to cover in class than it is in print. I teach the basic elements of Java to both my 6th-8th grade classes and my high school classes. Here's how things went today, in my 6th-8th grade class:

I introduced the class to what we were going to do today (write a simple program in Java, compile it and run it.) I had the students open a terminal window and a text editor then started writing out the HelloWorld program pretty much as it appears above. I explained each word as I wrote, as I do in the articles linked above.

Along the way, I got class interaction to make sure they were with me, that I wasn't just talking to the air. It gave some of them the chance to affirm their ideas about what I was telling them, others that had been a bit lost got it when we reviewed it.

I had them enter each line in their editor when I finished writing each line on the board. I explained the value of proper indentation, capitalization and the parens as part of the naming of different elements in Java (class names, method names, constructor methods, member fields.)

The class was able to anticipate the last curly brace in the program, and the lack of any indentation. It's great when they can already see where you're going.

Then I went over how to save and name the file, having them use 'pwd' in their terminal window to make sure they saved the file in that directory from the text editor. After saving, they used 'ls' to see that the file was there (one student caught an error that way.) pwd and ls were commands they were already familiar with, as well as general use of the command line.

Then I had them use 'javac' at the command line to compile the program. A few students had capitalization errors on the class name, and there were some missing semicolons. After a moment, the files had been corrected, saved, and everyone had a successful compile (we reviewed the "no news is good news" rule for the Unix command line.)

We then used 'java' to run our programs.

Elapsed time: 30 minutes.

Did Anything Stick?

Then we reviewed the program, word by word. I pointed at each word (including each part of System.out.println() ), they told me what it meant. I let each student have a go before affirming what it meant. They nailed them all, except for static. That was the only word that they were fuzzy on. In some cases they had a sense, but couldn't express it clearly. In other cases they just plain weren't sure.

So we reviewed what "static" does in the program.

Then I told them to change what the program prints, and add additional program lines to print things of their own.

They went to their editors, I hovered over. A couple of students needed reminders that the program needed to be recompiled once they'd saved an edited version.

Then they all had programs that had been customized, run with no errors.

Total elapsed class time: 40 minutes.

Then we talked about the differences between the source code and byte code files (see The Code Code), looked at the .java and .class files with 'cat' and 'strings' (a new command to them).

After looking at the .class file with 'strings', we talked about where the extra lines like '/java/io/PrintStream' came from and what they did. They nailed it, but then we've talked a lot about I/O in class.

I did a quick review of all the words in HelloWorld.java one more time, then spent the last few minutes of class covering what we'll be doing in future classes.

Finally, I asked them how complicated they thought today's lesson had been. The consensus was "pretty complicated, but not too complicated." I asked for votes for "really complicated" and got none.

Total elapsed class time: 55 minutes.


Part of the reason for getting so much done so quickly comes from the value of face to face interaction between student and instructor. This doesn't happen with books in most circumstances.

Part of the reason for success like this comes from preparation. The students already knew the basic tools of the command line and the text editor. The commands 'javac', 'java', and 'strings' were new in this class.

The students had previously edited HTML by hand. Want to talk complicated? Try this:
<title>Hello, World!</title>
<h1>Hello, World!</h1>
<p>This is my first web page.</p>
We do that in one class in the first semester.
By the end of the class we've had time to either add images or start styling our text. Kinda helps put the whole "boilerplate code" issue of Java into perspective, doesn't it? I seldom hear it claimed that HTML is too complicated for beginners to learn.

We've also done some programming in BASIC this year. The old GWBASIC, in fact. No graphics or sound, but just some simple text and calculation programs (like an insult generator and a die roller) inside the GWBASIC interpreter environment.

I drew on all these things, as well as our other discussions about the internals of the computer system and its OS in the course of this lesson.

Will It Still Be There Next Time?

If I don't follow up with anything else, no, of course it wouldn't stick as anything other than an exposure experience. But there will be follow up.

My 6th-8th grade classes meet once a week. One week is a long time for kids at this age. My high school classes meet twice a week, for 85 minutes per class. I cover a lot more material, faster, with these students.

Based on past experiences teaching this same material, I expect most of the class to remember what the program does, and the meanings of the following words once they see the program again:

void (for some reason this one seems to stick)

About half the class will remember the function of the curly braces versus the parens. About half (usually the other half) will remember the capitalization rules.

One or two will really remember 'static', the rest will need a refresher on that.

Which, for students from 11 to 14 years old, with only one hour a week, is pretty darn good. When I originally designed these lessons, I expected to repeat all the material from the first lesson in a slightly different way in a second lesson to get even half the material to stick.

As it is, the second lesson usually consists of about 10 minutes of review from HelloWorld.java, then we go on to introduce the Scanner and start accepting keyboard input into our programs (see Getting Keyboard Input for Java Apps, we end up with a program pretty close to TrollTalk.java.)

Is Java Too Complicated?

What do my students say to the assertion that Java is too complicated for a beginner to learn?

They laugh at it.

People who assert that the boilerplate in a simple Java application is too much have either had a poor exposure to the language themselves (please check out my other blog at A Beginning Programmer's Guide to Java, I wrote the site just for you), or they're advanced programmers making mountains out of speed bumps as stalking-horse arguments against Java.

Java isn't simple, but it's not too complex for a beginner.

p.s.-I love Hacker News myself, and take all my students there to open it up to them.


  1. It may not be hard, but personally I feel it is a waste of time. Just take a look at how much of this blog post is focused on *syntax*. That is the least interesting thing when it comes to starting programming. It is far more interesting to see recursion for the first time, or an intricate data structure. But when you have to spend so much time on pointless syntax, programming comes across as boring an unintuitive. Take a look at books like The Haskell Road to Logic, Math, and Programming or the Structure and Interpretation of Computer Programming lectures. They dive right into interesting concepts right away, because these languages were designed with minimal syntax in mind.

  2. Here's a little Java class I used with my students to get data into and out of their programs. It uses JOptionPane and let them make an actual GUI program:

    I never liked messing with Scanner or worse yet rolling my own text mode.
    Nice post! Even after this, you'll still have some people who'll tell you that this is the worst thing you could do to budding programmers.

  3. Yes I also agree that Java is not for beginners in programming. I used to hate Java when in college but had to learn it when I got job.

    By far Java has the most number of job opportunities.


  4. I spend the most time here talking about syntax since that's what's most at issue with the discussion of whether a Java Hello, World! program is too complex for beginners. Actually, those words have a function, even if there's little utility in Hello, World! They're not just boilerplate.

    As to getting to other concepts, there are not a lot of concepts that can be fit into an early class on any language. Even BASIC's 10 PRINT "HELLO" takes most of a class (starting the interpreter, the command line vs. direct statements, the quotes--they all need explanation.)

    Personally, I've found which language is used to introduce programming is not very important in early lessons. It's where you end up at the end of the class year that affects the choice of language the most.