Anatomy of a simple Java program

 

Java development software

In order to code and test Java programs, you need Java development software and must know how to use it. Before you go on, you MUST read and master the essential skill of running Java programs.

 

A sample Java program

The following program is a slight variation of the program that is part of the original Test project. When executed, it will display the message "Hello World!" on the system output device (the computer console):

/**
* This class defines a program that displays a message on the console.
*
* Written by: Jon Huhtala
*/

public class App {

  // This required method is the starting point for processing.

  public static void main(String[] args) {
    System.out.println("Hello World!"); // Display the message.
  }
}

 

Testing the sample program

Assuming you have a Test project and have already opened a Notepad window and a Command Prompt window, the procedure to test this sample program is as follows:

  1. "Copy" the source statements (shown in blue above).

  2. "Paste" the source statements into your Notepad window being sure to replace the entire contents of the window if it contained previous code.

  3. Save the source file under the name "App.java" in your Test project.

  4. Compile the "App.java" source file by entering the following command in the Command Prompt window:

javac App.java

  1. Run the "App.class" bytecode file by entering the following command in the Command Prompt:

java App

 

Program analysis

Studying this sample program line-by-line will start you on the path to learning Java. The sections that follow will help you analyze the sample program. For now, it is not important that you understand everything completely. Through constant usage, you will eventually know what it all means.

 

Comments in Java

  1. Javadoc. Begin with /** and end with */. Everything between is treated as a comment.

  2. Single-line. Begin with // and automatically continue to the end of the line.

  3. Multi-line. Begin with /* and end with */. Everything between is treated as a comment.

 

The program's class

public class App

is the class header. It declares that a class named App is to be defined and that it is to be publicly available for access from other places. In order for the Java Virtual Machine to find and use the program's class, always make it public. Other access modifiers (private, protected, and default or package access) will be presented later.

 

Statement blocks

 

The main method

public static void main(String[] args)

This states that the method has public access, is a class (static) method not to be associated with any objects of the class, does not return a value to the caller (void), has the name main, and receives an array of string references (String[]) named args as parameters. It is not important that you understand all this now, just code the statement exactly as shown. The only flexibility you have in coding the header is the name of the array. The sample program will run just fine if you re-code the method header as

public static void main(String[] x)

 

Writing to the system output device (the console)

 

Compiler requirements

DO NOT write code like this

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

even though it compiles and runs just fine. Try it and see!

 

Review questions

  1. Which of the following are invalid headers for the main method  (choose three)

  1. public static void Main(String[] args)

  2. public static void main(String[] parms)

  3. public static void main(String args)

  4. public static void main(string[] args)

  5. public static void main(String[] args)

  1. What will result from attempting to compile and execute the following?

public class App
{
  public static void main(String[] parms)
  {
    // System.out.println("Java is fun");
  }
}

  1. it will not compile

  2. it will compile but an error will occur at run time

  3. it will compile and run but nothing will display

  4. it will compile and run to display the message "Java is fun" on the console

  1. What will result from attempting to compile and execute the following?

public class App {
  public static void main(String[] parms) {
    System.out.println("Java is fun");
  }
}

  1. it will not compile

  2. it will compile but an error will occur at run time

  3. it will compile and run but nothing will display

  4. it will compile and run to display the message "Java is fun" on the console

  1. What will result from attempting to compile and execute the following?

public class App
{
  public static void main(String[] args)
  {
    System.out.println("Java is fun")
  }
}

  1. it will not compile

  2. it will compile but an error will occur at run time

  3. it will compile and run but nothing will display

  4. it will compile and run to display the message "Java is fun" on the console