Notatypewriter's Blog

Umm… what?

Teaching Java Fail

leave a comment »

I tried to help somebody who had never programmed before with his Java homework. It opened my eyes to what seems to be Java’s failings as a first language for non-programmers.

Ithink the problem is that the best way to introduce programming seems to be little script-y things and then build up to packages and classes. Java does not really enable that. Here are some examples.

Java is heavy with syntax. My student had several questions relating to syntax. Here’s the example he was struggling over.

package com.notatypewriter.playground;

import java.util.*;

public class ScannerPrintEachInt
{
	public static void main(String[] args) {

		int count, sum = 0, num;

		Scanner input = new Scanner(System.in);

		for (count = 0; count < 5; count++) {
			num = input.nextInt();
			sum += num;
		}

		System.out.println("sum is " + sum);
		System.out.println("count is " + count);
	}
}

The biggest fail was the for loop. I hadn’t realized how confusing this syntax was. I don’t remember ever having any problems understanding for loops, so this hit me like a ton of bricks. A new programmer’s model of a program is like a line-by-line interpreter. He reads one line and updates his understanding of the world, then repeats on the next line. So if he reads

for (count = 0; count < 5; count++)

he probably believes these three statements happen one right after the other, which is incorrect. Furthermore, it is not immediately clear what the bounds of count are unless the programmer iterates through the loop in his head until he learns the idiom. The problem is that this for loop looks too much like normal code. Let’s look at this same thing in Python syntax.

for count in range(0, 5):

or Matlab:

for count=0:4

These syntaxes are much less ambiguous. In either syntax, we can unambiguously see that count ranges between two values. Python is arguably less clear due to 0-indexing but at least it’s in basically English. Matlab probably has the least ambiguous syntax.

The next thing I had to explain was the integer declarations. This led into a discussion of integer sizes and memory. Do I want to be explaining this to a newbie programmer? Consider that the programmer doesn’t really care. He just wants to take input and add his numbers.

He also had questions regarding the package declarations on line 1, the import statements on line 3, and the access modifiers on the class and the main() method. I wasn’t really comfortable explaining scoping and software engineering best practices to a programming newbie so I basically had to tell him to keep putting it there for now and I can explain it when his understanding of Java increases. (I’ve always been taught in the same way so I’m not the only one who believes this.)

This was unsatisfying to my student. He’s trying his best to understand this black box in front of him and the one person who can help him has to say “no”.

Written by notatypewriter

2011 November 16 at 11:57 pm

Posted in Nerding out

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: