Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Spring 2015
Class Notes—January 26, 2014
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes |


Java boilerplate

All Java programs have the following structure:

public class Hello {
(... source code goes here ... )

In order to have your program actually do something, you need a "main" definition in it. (This isn't strictly true, as we'll see when we get to object definitions, but that's several weeks ahead.) This brings the total boilerplate code to:

public class Hello { public static void main(String[] args) {
(... source code goes here ... )
} }

Commit this much to memory, as you'll want it to be automatic whenever you write a new program.


Simple statements: output

Inside the { and } of main(), you can have any legal statements. Each statement must be terminated by the semicolon, and the statements are run in the order they appear in the source code (unless you add control constructs, which we'll see in a couple of weeks).

The simplest useful statement is probably this one:

System.out.println("Hi, there!");

This prints whatever occurs between the double quotes to the screen (in the terminal window where you're running the program), following by a line break. Here's a complete program example:

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


Compiling source code and executing programs from the command line

We briefly reviewed the concept of the command line interface. More detailed notes are available as part of the Math/CS department's general-purpose tutorial, available at

The Linux environment and the command line interface in particular are the subjects of Lab #1.

To compile your Java source code file into an executable, you can use the javac command on the command line. First navigate to the folder (a.k.a. "directory") containing the file. Study the commands cd and ls to learn how to do this. Then:

john-lasseter:code jlasseter$  javac
john-lasseter:code jlasseter$  

If your program has no errors, no messages will be generated, and you'll get another prompt (as above). Silently, however, an executable version of your program, Hello.class, has been created:

john-lasseter:code jlasseter$  ls
Hello.class	archive		file_examples	index.html
john-lasseter:code jlasseter$ 

We can run this by invoking the Java Runtime Environment, using the java command:

john-lasseter:code jlasseter$  java Hello
Hi, there!
john-lasseter:code jlasseter$ 

Note that we only invoke the program as "Hello", not "Hello.class", even though the latter is the full name of the executable file.


Input: reading arguments from the command line

If we want to run our program with additional command-line arguments, such as

john-lasseter:code jlasseter$  java Hello Sparky Wagner

we can modify our source code to use these additional arguments. The trick is to use the resource args, which appears in the boilerplate for main(). Basically, when you invoke a program with additional arguments as above, these arguments are stored in args, one at a time, and you can access them by number. They're numbered from 0, not 1, so the first argument (i.e. the "zeroth") is stored in args[0], the next one in args[1], and so on. For example,

public class Hello { public static void main(String[] args) { System.out.println("Hi there, " + args[0] + " the wonder cat!"); System.out.println("How are you today, Ms. " + args[1] + "?"); } }

Re-compiling this and running it, we have:

john-lasseter:code jlasseter$  javac
john-lasseter:code jlasseter$  java Hello Sparky Wagner  
Hi there, Sparky the wonder cat!
How are you today, Ms. Wagner?
john-lasseter:code jlasseter$  

Try this out with two arguments (as above), fewer than two, and more than two. What happens in each case?

John Lasseter