Why Java is not good as a first language

During the spring semester of the last year, a friend of mine was trying to learn Java as part of her studies. I think it’s great when anybody shows an interest in programming so I of course volunteered to help her out a bit. That was an interesting experience in many ways.

On the one hand I found it very difficult to find ways to explain in plain and simple terms concepts that after decades of programming feel like second nature to me. Another challenge was of course to guide her towards understanding without giving her the answer to her excercises, especially when the email exchanges got rapid and I sensed the frustration rise at the other end.

On the other hand though, the language itself also created some obstacles to learning in my opinion. To understand why, let’s look at the seminal “Hello, World”-sample program:

import java.lang.*;

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

There’s not many lines of code here, and the gist of it is not hard to comprehend even for a beginning programmer. But there’s a lot going on here that unnecessarily obstructs the understanding of the code.

To fully understand this code, you need to be familiar with classes, objects, methods, accessibillity, arrays, packages and streams. That’s a very tall order for the first day of class. So what the teacher does is to teach the students to ignore the stuff they haven’t learned yet:

  • Why do you have to write public class Hello? Don’t worry about that, just copy it for now.
  • What’s the public static void main-thing? It’s where you should write your program, just ignore it for now.
  • What about the args? …

And they have to. There’s no way you can get around this code without ignoring pretty much everything in it. That’s bad for many reasons.

Programming is about paying attention to details, not ignoring them. So this very way of introducing someone to programming encourages habits you have to unlearn again as you advance. Another reason this is bad is that since the students won’t understand it, they will not be able to write it from scratch either. This again encourages them to use crutches like editor macros for generating code templates, or simply by copying something that works from somewhere else.

Now contrast this to a programming language that was designed for learning:

program Hello;

begin
    writeln('Hello, World!');
end.

This is the same program written in Pascal1. There’s essentially nothing that even the novice programmer can not grasp here. You declare the name of the program, and write the code of your program between the begin and end.-statements. Output to the terminal is handled by a simple call to writeln, there’s no need to know about streams, functions or anything else.

Now it’s easy to expand by introducing variables, loops, conditionals, functions etc. Along each step the students should be able to follow without having to repeat, memorize or copy incantations of unintelligible code that just has to be there. Once the basic concepts are learned properly it should be much easier to move to new languages, as it will now mostly amount to mapping the same concepts to a different syntax.

It can of course be argued that “Hello world” is not a realistic example of programs that you will write as a student. You will quickly move beyond this, but to learn the basics the extra stuff still gets in the way. By removing the cruft and allowing the students to build understanding one concept at the time, they have a far better chance of learning anything at all. And there will be less bad habits to unlearn afterwards.

Neither is it my intention to single out Pascal as the perfect teaching language here. Ruby, Python and perhaps even JavaScript are also good candidates, as are numerous other modern languages. In fact I don’t think the language itself is all that important. The important thing is that the student can understand the code she writes even from the very beginning. And that you can progress from there without having to bite over too many concepts at the time.

1

This is quite likely the first Pascal code I’ve written in 25 years!