Published on ONJava.com (http://www.onjava.com/)
 See this if you're having trouble printing code examples

Rethinking the Java Curriculum: Goodbye, HelloWorld!

by Daniel H. Steinberg

This is the first in a series of articles that looks at how programming in Java is taught to new programmers. In part, this series proposes to introduce object-oriented programming using the programming-centric practices of Extreme Programming (XP). This is not an argument of whether XP is the right methodology for you to use when developing your applications; instead, we'll rethink what new programmers should learn in an introductory course in Java. Please join the discussion at the end of each article and feel free to suggest future topics in the forum or by emailing me at DSteinberg@core.com.

When the Java programming language was new, introductory books, courses, and online tutorials stressed that the C-like syntax would make it easy for experienced programmers to move to Java. Sure, there were books that focused on the advantages of object-oriented programming and promised that inheritance would save the world. But, after a brief history that included some references to Oak, set-top boxes, and the Web, most introductions were ports of existing books on C and C++.

We didn't have to throw out our copies of Kernighan and Ritchie, we could just learn this extra "object stuff." Somehow, Java was supposed to be better. We were told that Java didn't have any pointers, and yet many of us, within hours of downloading the JDK, immediately threw a NullPointerException. As we learned more and more about programming in Java, we found that C was not the right way to approach Java. That doesn't mean the early books were wrong; that's what we needed to hear at the time. Those resources feel dated now because many of the newcomers to Java are newcomers to programming. We don't need to explain to them how to get to Java from C -- we get to rethink the best way to approach programming, if Java is the first language.

This is a particularly interesting time to rethink our approach. Starting in September 2003, the High School Advanced Placement exam in Computer Science will be available in Java. Introductory courses at colleges are being taught in Java, and educators have put a lot of effort into rethinking the curriculum. One project led by Lynn Andrea Stein has resulted in the Rethinking CS101 Project and its upcoming Web site. Stein has moved from the AI lab at MIT to the newly-created Olin College.

A Non-OO Intro to OO

If you were allowed to begin a course on object-oriented programming using Java, where would you begin? An obvious choice is:

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

As Jason Hunter notes in his Java Servlet Programming book (O'Reilly, 2nd edition 2002), HelloWorld has been used as a first program since the B language (the predecessor to the C language). Introductions to everything from Java to JSPs and Servlets to Ruby to JDBC start with some sort of HelloWorld example program. It's a short, simple-to-write program with an immediately observable result. A novice user can be led through the editing tools to enter and save the half a dozen lines of source code. The beginner can then be told how to compile and run the program. Even a newbie knows that the tasks have been successfully completed when the words "Hello World" appear in the console window.

Related Reading

Learning Java
By Patrick Niemeyer, Jonathan Knudsen

On the other hand, there are many reasons to view HelloWorld as the worst possible way to begin a course in object-oriented programming. At its core, an object-oriented program should consist of highly-cohesive objects sending and receiving messages to other objects. Maybe when a user clicks on a JButton that is part of the application's GUI, all of the objects interested in knowing when this particular JButton is clicked will be notified. These objects then do what needs to be done -- perhaps by sending messages to other objects.

Now look back at HelloWorld. No objects are created. The HelloWorld class doesn't even contain any method, other than main() -- that's a rant I'll save for the next section. HelloWorld is a class without any reason to exist, and yet this is where we begin our discussion of object-oriented programming. If you were highly charitable, you might give HelloWorld OO points because the println() method of the out class in the System package is being invoked. Blech.

The Big Bang

So an OO program is all about objects interacting with each other by sending messages back and forth. A pedagogical dilemma is that all of this interacting has to start somewhere, but this isn't where the explanation of object-oriented programming should begin. In other words, you need to have a main() method to start a program, but you don't want to start the course by explaining all of the components of main().

Similarly, in a Physics course, we begin by explaining how physical objects interact with each other. We don't begin by explaining the Big Bang and the creation of the universe. It's much easier to demonstrate and explain Newton's laws of motion. In a Java program, the "big bang" that creates the first object is the main() method:

public static void main( String [] args );

You could start the class by explaining what each keyword means. This means you'll have to begin the class by explaining:

Sigh. The main() method might be the right place to start the execution of a Java program, but it's not the right place to start your explanation of a Java program.

The Challenge

So what do you think? Are you ready to give up on HelloWorld as a first program, or do you see benefits that you would like to argue in favor of? What about main()? You need main() or your program won't run. How do you approach it? Do you tell your students to "just type it in verbatim, you learn what all of the terms mean later"? Do you explain each of the terms and then continue the semester with the three people that haven't dropped your class?

How would you begin a Java-based introduction to object-oriented programming?

Daniel H. Steinberg is the editor for the new series of Mac Developer titles for the Pragmatic Programmers. He writes feature articles for Apple's ADC web site and is a regular contributor to Mac Devcenter. He has presented at Apple's Worldwide Developer Conference, MacWorld, MacHack and other Mac developer conferences.

Return to ONJava.com.

Copyright © 2009 O'Reilly Media, Inc.