ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Rethinking the Java Curriculum: Goodbye, HelloWorld!

by Daniel H. Steinberg
08/21/2002

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:

  • Accessors: Before students even understand the difference between objects and classes, before they have even seen a program with more than one object or class, you have to explain levels of access. The HelloWorld class isn't even in a package, and yet you'll be forced to discuss packaging and hierarchies in order to distinguish among public, protected, private, and that access level that doesn't even have a name.

  • Class methods: The use of the static keyword depends on students having an understanding of which methods and attributes should belong to a class and which should belong to an object. As mentioned before, at this point, students don't see the difference between classes and objects. (If you are an experienced Java developer, you may not remember how difficult it is to grasp this distinction.) Note that the HelloWorld class has a single method. Whether or not main() is static is not based so much on the conceptual differences between a class and an object as it is on whether the main() method needs to be called before an object of type HelloWorld is created.

  • Return type: Imagine it is your task to explain the return type of a method. You may choose something like int add(int i, int j) to illustrate that the add() method will return the sum of i and j as an int. You would not choose a method that doesn't return anything as your first example. You wouldn't introduce void as your first return type any more than you would try to explain multiplication starting with examples of numbers multiplied by zero.

  • Command line arguments: The parameters of the main() method are command-line arguments. Often the example that follows HelloWorld is a refinement that allows the user to type in java HelloWorld myName and get the feedback Hello myName. Do you really want to cover these right away? For this and the other items in this list, the question isn't whether these are important topics, but rather whether they are the topics that would best be presented first in a course designed to teach students object-oriented programming in Java.

  • Arrays of Strings: The args variable is a String array. I don't want to beat this example to death, but you don't want to have to introduce arrays in order to adequately explain the signature of this method. You also don't want to have to explain that if you want to read in an int, then you read it in as a String and convert it to an int by using another static method in a wrapper class.

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.