Anatomy of a Java Program | Heaton Research

Anatomy of a Java Program

Get the entire book!
Java for the Beginning Programmer

Source code is what you “type in” to create a program. The compiler takes source code and creates a program from it that the computer can execute. As a programmer, it will be your job to create this source code. We used some simple source code in the previous section, now lets take a look at a slightly more complex program and see what goes into its source code.

Listing 3.1 shows a simple Java application that asks you what your name is, and then says hello to you.

Listing 3.1: Sample Java Program (UserInput.java)

import java.io.*;

public class UserInput
{

  /**
   * Main entry point for example.
   * @param args Not used.
   */
  public static void main(String args[])
  {
    try
    {
      BufferedReader userInput = 
        new BufferedReader(
        new InputStreamReader(System.in));

      System.out.print("What is your name? ");
      String str = userInput.readLine();
      System.out.println("Hello " + str);
    }
    catch(IOException e)
    {
      System.out.println(“IO Exception”);
    }
  }
}

We will examine this program from the top, beginning with the import statements.

Copyright 2005-2008 by Heaton Research, Inc.